2
KSysGuard, the KDE System Guard
4
Copyright (c) 2001 Tobias Koenig <tokoe@kde.org>
6
This program is free software; you can redistribute it and/or
7
modify it under the terms of version 2 of the GNU General Public
8
License as published by the Free Software Foundation.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program; if not, write to the Free Software
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
#include <config-workspace.h>
27
#include <sys/statvfs.h>
35
#include "ksysguardd.h"
40
struct statvfs statvfs;
43
static CONTAINER DiskStatList = 0;
44
static CONTAINER OldDiskStatList = 0;
45
static struct SensorModul* DiskStatSM;
46
char *getMntPnt( const char* cmd );
48
static void sanitize(char *str) {
52
if(*str == '\t' || *str == '\n' || *str == '\r' || *str == ' ' || !isascii(*str) )
58
char *getMntPnt( const char* cmd )
60
static char device[ 1025 ];
63
memset( device, 0, sizeof( device ) );
64
sscanf( cmd, "partitions%1024s", device );
66
ptr = (char*)rindex( device, '/' );
72
/* ----------------------------- public part ------------------------------- */
74
static char monitor[ 1024 ];
75
static void registerMonitors(const char* mntpnt) {
76
snprintf( monitor, sizeof( monitor ), "partitions%s/usedspace", mntpnt );
77
registerMonitor( monitor, "integer", printDiskStatUsed, printDiskStatUsedInfo, DiskStatSM );
78
snprintf( monitor, sizeof( monitor ), "partitions%s/freespace", mntpnt );
79
registerMonitor( monitor, "integer", printDiskStatFree, printDiskStatFreeInfo, DiskStatSM );
80
snprintf( monitor, sizeof( monitor ), "partitions%s/filllevel", mntpnt );
81
registerMonitor( monitor, "integer", printDiskStatPercent, printDiskStatPercentInfo, DiskStatSM );
83
static void removeMonitors(const char* mntpnt) {
84
snprintf( monitor, sizeof( monitor ), "partitions%s/usedspace", mntpnt );
85
removeMonitor( monitor );
86
snprintf( monitor, sizeof( monitor ), "partitions%s/freespace", mntpnt );
87
removeMonitor( monitor );
88
snprintf( monitor, sizeof( monitor ), "partitions%s/filllevel", mntpnt );
89
removeMonitor( monitor );
92
void initDiskStat( struct SensorModul* sm )
97
OldDiskStatList = NULL;
99
if ( updateDiskStat() < 0 )
102
registerMonitor( "partitions/list", "listview", printDiskStat, printDiskStatInfo, sm );
104
for ( disk_info = first_ctnr( DiskStatList ); disk_info; disk_info = next_ctnr( DiskStatList ) ) {
105
registerMonitors(disk_info->mntpnt);
109
void exitDiskStat( void )
113
removeMonitor( "partitions/list" );
115
for ( disk_info = first_ctnr( DiskStatList ); disk_info; disk_info = next_ctnr( DiskStatList ) ) {
116
removeMonitors(disk_info->mntpnt);
119
destr_ctnr( DiskStatList, free );
121
destr_ctnr( OldDiskStatList, free );
124
void checkDiskStat( void )
127
DiskInfo* disk_info_new;
128
DiskInfo* disk_info_old;
130
for ( disk_info_new = first_ctnr( DiskStatList ); disk_info_new; disk_info_new = next_ctnr( DiskStatList ) ) {
132
for ( disk_info_old = first_ctnr( OldDiskStatList ); disk_info_old; disk_info_old = next_ctnr( OldDiskStatList ) ) {
133
if(strcmp(disk_info_new->mntpnt, disk_info_old->mntpnt) == 0) {
134
free( remove_ctnr( OldDiskStatList ) );
140
/* register all the devices that did not exist before*/
141
registerMonitors(disk_info_new->mntpnt);
145
/*Now remove all the devices that do not exist anymore*/
146
for ( disk_info_old = first_ctnr( OldDiskStatList ); disk_info_old; disk_info_old = next_ctnr( OldDiskStatList ) ) {
147
removeMonitors(disk_info_old->mntpnt);
150
destr_ctnr( OldDiskStatList, free );
151
OldDiskStatList = NULL;
154
print_error( "RECONFIGURE" ); /*Let ksysguard know that we've added a sensor*/
157
int updateDiskStat( void )
161
struct mntent *mnt_info;
163
if ( ( fh = setmntent( "/etc/mtab", "r" ) ) == NULL ) {
164
print_error( "Cannot open \'/etc/mtab\'!\n" );
167
if(OldDiskStatList == 0) {
168
OldDiskStatList = DiskStatList;
169
DiskStatList = new_ctnr();
171
empty_ctnr(DiskStatList);
174
while ( ( mnt_info = getmntent( fh ) ) != NULL ) {
176
* An entry which device name doesn't start with a '/' is
177
* either a dummy file system or a network file system.
178
* Add special handling for smbfs and cifs as is done by
181
if ( (mnt_info->mnt_fsname[0] != '/') ||
182
!strcmp( mnt_info->mnt_type, "smbfs" ) ||
183
!strcmp( mnt_info->mnt_type, "cifs" ) ||
184
!strcmp( mnt_info->mnt_type, "proc" ) ||
185
!strcmp( mnt_info->mnt_type, "devfs" ) ||
186
!strcmp( mnt_info->mnt_type, "usbfs" ) ||
187
!strcmp( mnt_info->mnt_type, "sysfs" ) ||
188
!strcmp( mnt_info->mnt_type, "tmpfs" ) ||
189
!strcmp( mnt_info->mnt_type, "devpts" ) )
190
continue; /* Skip these file systems */
192
if ( ( disk_info = (DiskInfo *)malloc( sizeof( DiskInfo ) ) ) == NULL )
195
memset( disk_info, 0, sizeof( DiskInfo ) );
197
if ( statvfs( mnt_info->mnt_dir, &(disk_info->statvfs) ) < 0 )
200
strncpy( disk_info->device, mnt_info->mnt_fsname, sizeof( disk_info->device ) );
201
disk_info->device[ sizeof(disk_info->device) -1] = 0;
203
strncpy( disk_info->mntpnt, mnt_info->mnt_dir, sizeof( disk_info->mntpnt ) );
204
disk_info->mntpnt[ sizeof(disk_info->mntpnt) - 1] = 0;
205
sanitize(disk_info->mntpnt);
207
push_ctnr( DiskStatList, disk_info );
214
int calculatePercentageUsed( unsigned long totalSizeKB, unsigned long available) {
218
unsigned long totalSizeKBdividedBy100 = (50 + totalSizeKB )/ 100;
219
if (!totalSizeKBdividedBy100)
222
int percentageUsed = 100 - available / totalSizeKBdividedBy100; /* Percentage is 1 - available / totalSizeKB, meaning that we count root-only reserved space as "used" here */
223
/* If we have rounded down to 0%, make it 1%, like "df" does */
224
if (percentageUsed == 0)
226
return percentageUsed;
229
void printDiskStat( const char* cmd )
234
for ( disk_info = first_ctnr( DiskStatList ); disk_info; disk_info = next_ctnr( DiskStatList ) ) {
235
/* See man statvfs(2) for meaning of fields */
236
unsigned long totalSizeKB = disk_info->statvfs.f_blocks * (disk_info->statvfs.f_frsize/1024);
237
unsigned long usedKB = totalSizeKB - (disk_info->statvfs.f_bfree * (disk_info->statvfs.f_bsize/1024)); /* used is the total size minus free blocks including those for root only */
238
unsigned long available = disk_info->statvfs.f_bavail * (disk_info->statvfs.f_bsize/1024); /* available is only those for non-root. So available + used != total because some are reserved for root */
239
int percentageUsed = calculatePercentageUsed(totalSizeKB, available);
240
output( "%s\t%ld\t%ld\t%ld\t%d\t%s\n",
252
void printDiskStatInfo( const char* cmd )
255
output( "Device\tSize\tUsed\tAvailable\tUsed %%\tMount point\nM\tKB\tKB\tKB\t%%\ts\n" );
258
void printDiskStatUsed( const char* cmd )
260
char *mntpnt = (char*)getMntPnt( cmd );
263
for ( disk_info = first_ctnr( DiskStatList ); disk_info; disk_info = next_ctnr( DiskStatList ) ) {
264
if ( !strcmp( mntpnt, disk_info->mntpnt ) ) {
265
unsigned long totalSizeKB = disk_info->statvfs.f_blocks * (disk_info->statvfs.f_frsize/1024);
266
unsigned long usedKB = totalSizeKB - (disk_info->statvfs.f_bfree * (disk_info->statvfs.f_bsize/1024)); /* used is the total size minus free blocks including those for root only */
267
output( "%ld\n", usedKB );
274
void printDiskStatUsedInfo( const char* cmd )
277
output( "Used\t0\t0\tKB\n" );
280
void printDiskStatFree( const char* cmd )
282
char *mntpnt = (char*)getMntPnt( cmd );
285
for ( disk_info = first_ctnr( DiskStatList ); disk_info; disk_info = next_ctnr( DiskStatList ) ) {
286
if ( !strcmp( mntpnt, disk_info->mntpnt ) ) {
287
unsigned long available = disk_info->statvfs.f_bavail * (disk_info->statvfs.f_bsize/1024); /* available is only those for non-root. So available + used != total because some are reserved for root */
288
output( "%ld\n", available );
294
void printDiskStatFreeInfo( const char* cmd )
297
output( "Available\t0\t0\tKB\n" );
300
void printDiskStatPercent( const char* cmd )
302
char *mntpnt = (char*)getMntPnt( cmd );
305
for ( disk_info = first_ctnr( DiskStatList ); disk_info; disk_info = next_ctnr( DiskStatList ) ) {
306
if ( !strcmp( mntpnt, disk_info->mntpnt ) ) {
307
unsigned long totalSizeKB = disk_info->statvfs.f_blocks * (disk_info->statvfs.f_frsize/1024);
308
unsigned long available = disk_info->statvfs.f_bavail * (disk_info->statvfs.f_bsize/1024); /* available is only those for non-root. So available + used != total because some are reserved for root */
310
int percentageUsed = calculatePercentageUsed(totalSizeKB, available);
311
output( "%d\n", percentageUsed );
318
void printDiskStatPercentInfo( const char* cmd )
321
output( "Percentage Used\t0\t100\t%%\n" );