1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
/***************************************************************************
* Copyright (C) 2003 by Hans Karlsson *
* [email protected] *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include "disksensor.h"
#include <tqfile.h>
#include <tqtextstream.h>
#include <tqstring.h>
#include <tqregexp.h>
#include <kprocess.h>
#include <kprocio.h>
DiskSensor::DiskSensor( int msec ) : Sensor( msec )
{
connect(&ksp, TQT_SIGNAL(receivedStdout(KProcess *, char *, int )),
this,TQT_SLOT(receivedStdout(KProcess *, char *, int )));
connect(&ksp, TQT_SIGNAL(processExited(KProcess *)),
this,TQT_SLOT(processExited( KProcess * )));
// update values on startup
ksp.clearArguments();
ksp << "df";
ksp.start( KProcIO::Block,KProcIO::Stdout);
init = 1;
}
DiskSensor::~DiskSensor()
{}
long DiskSensor::getFreeSpace(TQString mntPt) const
{
TQRegExp rx( "^\\S*\\s*\\d+\\s+\\d+\\s+(\\d+)");
rx.search(mntMap[mntPt]);
return rx.cap(1).toLong();
}
long DiskSensor::getUsedSpace(TQString mntPt) const
{
TQRegExp rx( "^\\S*\\s*\\d+\\s+(\\d+)\\s+\\d+");
rx.search(mntMap[mntPt]);
return rx.cap(1).toLong();
}
long DiskSensor::getTotalSpace(TQString mntPt) const
{
TQRegExp rx( "^\\S*\\s*(\\d+)\\s+\\d+\\s+\\d+");
rx.search(mntMap[mntPt]);
return rx.cap(1).toLong();
}
int DiskSensor::getPercentUsed(TQString mntPt) const
{
TQRegExp rx( "\\s+(\\d+)%\\s+");
rx.search(mntMap[mntPt]);
return rx.cap(1).toInt();
}
int DiskSensor::getPercentFree(TQString mntPt) const
{
return ( 100 - getPercentUsed( mntPt ) );
}
void DiskSensor::receivedStdout(KProcess *, char *buffer, int len )
{
buffer[len] = 0;
sensorResult += TQString( TQCString(buffer) );
}
void DiskSensor::processExited(KProcess *)
{
TQStringList stringList = TQStringList::split('\n',sensorResult);
sensorResult = "";
TQStringList::Iterator it = stringList.begin();
//TQRegExp rx( "^(/dev/).*(/\\S*)$");
TQRegExp rx( ".*\\s+(/\\S*)$");
while( it != stringList.end())
{
rx.search( *it );
if ( !rx.cap(0).isEmpty())
{
mntMap[rx.cap(1)] = *it;
}
it++;
}
stringList.clear();
TQString format;
TQString mntPt;
SensorParams *sp;
Meter *meter;
TQObjectListIt lit( *objList );
while (lit != 0)
{
sp = (SensorParams*)(*lit);
meter = sp->getMeter();
format = sp->getParam("FORMAT");
mntPt = sp->getParam("MOUNTPOINT");
if (mntPt.length() == 0)
mntPt="/";
if (format.length() == 0 )
{
format = "%u";
}
format.replace( TQRegExp("%fp", false),TQString::number(getPercentFree(mntPt)));
format.replace( TQRegExp("%fg",false),
TQString::number(getFreeSpace(mntPt)/(1024*1024)));
format.replace( TQRegExp("%fkb",false),
TQString::number(getFreeSpace(mntPt)*8) );
format.replace( TQRegExp("%fk",false),
TQString::number(getFreeSpace(mntPt)) );
format.replace( TQRegExp("%f", false),TQString::number(getFreeSpace(mntPt)/1024));
format.replace( TQRegExp("%up", false),TQString::number(getPercentUsed(mntPt)));
format.replace( TQRegExp("%ug",false),
TQString::number(getUsedSpace(mntPt)/(1024*1024)));
format.replace( TQRegExp("%ukb",false),
TQString::number(getUsedSpace(mntPt)*8) );
format.replace( TQRegExp("%uk",false),
TQString::number(getUsedSpace(mntPt)) );
format.replace( TQRegExp("%u", false),TQString::number(getUsedSpace(mntPt)/1024));
format.replace( TQRegExp("%tg",false),
TQString::number(getTotalSpace(mntPt)/(1024*1024)));
format.replace( TQRegExp("%tkb",false),
TQString::number(getTotalSpace(mntPt)*8));
format.replace( TQRegExp("%tk",false),
TQString::number(getTotalSpace(mntPt)));
format.replace( TQRegExp("%t", false),TQString::number(getTotalSpace(mntPt)/1024));
meter->setValue(format);
++lit;
}
if ( init == 1 )
{
emit initComplete();
init = 0;
}
}
void DiskSensor::update()
{
ksp.clearArguments();
ksp << "df";
ksp.start( KProcIO::NotifyOnExit,KProcIO::Stdout);
}
void DiskSensor::setMaxValue( SensorParams *sp )
{
Meter *meter;
meter = sp->getMeter();
const TQString mntPt = sp->getParam( "MOUNTPOINT" );
TQString f;
f = sp->getParam("FORMAT");
if( f == "%fp" || f == "%up" )
meter->setMax( 100 );
else
meter->setMax( getTotalSpace( mntPt ) / 1024 );
}
#include "disksensor.moc"
|