Changeset View
Changeset View
Standalone View
Standalone View
ksysguardd/Linux/diskstat.c
Show First 20 Lines • Show All 67 Lines • ▼ Show 20 Line(s) | 61 | { | |||
---|---|---|---|---|---|
68 | ptr = strrchr( device, '/' ); | 68 | ptr = strrchr( device, '/' ); | ||
69 | if( ptr ) { | 69 | if( ptr ) { | ||
70 | *ptr = '\0'; | 70 | *ptr = '\0'; | ||
71 | } | 71 | } | ||
72 | 72 | | |||
73 | return (char*)device; | 73 | return (char*)device; | ||
74 | } | 74 | } | ||
75 | 75 | | |||
76 | unsigned long getTotal( const char* mntpnt ) | ||||
77 | { | ||||
78 | DiskInfo* disk_info; | ||||
79 | | ||||
80 | unsigned long total = 0; | ||||
81 | int is_all = strcmp( mntpnt, "/all" ) == 0; | ||||
82 | | ||||
83 | for ( disk_info = first_ctnr( DiskStatList ); disk_info; disk_info = next_ctnr( DiskStatList ) ) { | ||||
84 | if ( !strcmp( mntpnt, disk_info->mntpnt ) || is_all ) { | ||||
85 | unsigned long totalSizeKB = disk_info->statvfs.f_blocks * (disk_info->statvfs.f_frsize/1024); | ||||
86 | | ||||
87 | if ( is_all ) { | ||||
88 | total += totalSizeKB; | ||||
89 | } else { | ||||
90 | return totalSizeKB; | ||||
91 | } | ||||
92 | } | ||||
93 | } | ||||
94 | | ||||
95 | return total; | ||||
96 | } | ||||
97 | | ||||
76 | /* ----------------------------- public part ------------------------------- */ | 98 | /* ----------------------------- public part ------------------------------- */ | ||
77 | 99 | | |||
78 | static char monitor[ 1024 ]; | 100 | static char monitor[ 1024 ]; | ||
79 | static void registerMonitors(const char* mntpnt) { | 101 | static void registerMonitors(const char* mntpnt) { | ||
80 | snprintf( monitor, sizeof( monitor ), "partitions%s/usedspace", mntpnt ); | 102 | snprintf( monitor, sizeof( monitor ), "partitions%s/usedspace", mntpnt ); | ||
81 | registerMonitor( monitor, "integer", printDiskStatUsed, printDiskStatUsedInfo, DiskStatSM ); | 103 | registerMonitor( monitor, "integer", printDiskStatUsed, printDiskStatUsedInfo, DiskStatSM ); | ||
82 | snprintf( monitor, sizeof( monitor ), "partitions%s/freespace", mntpnt ); | 104 | snprintf( monitor, sizeof( monitor ), "partitions%s/freespace", mntpnt ); | ||
83 | registerMonitor( monitor, "integer", printDiskStatFree, printDiskStatFreeInfo, DiskStatSM ); | 105 | registerMonitor( monitor, "integer", printDiskStatFree, printDiskStatFreeInfo, DiskStatSM ); | ||
84 | snprintf( monitor, sizeof( monitor ), "partitions%s/filllevel", mntpnt ); | 106 | snprintf( monitor, sizeof( monitor ), "partitions%s/filllevel", mntpnt ); | ||
85 | registerMonitor( monitor, "integer", printDiskStatPercent, printDiskStatPercentInfo, DiskStatSM ); | 107 | registerMonitor( monitor, "integer", printDiskStatPercent, printDiskStatPercentInfo, DiskStatSM ); | ||
108 | snprintf( monitor, sizeof( monitor ), "partitions%s/total", mntpnt ); | ||||
109 | registerMonitor( monitor, "integer", printDiskStatTotal, printDiskStatTotalInfo, DiskStatSM ); | ||||
86 | } | 110 | } | ||
87 | static void removeMonitors(const char* mntpnt) { | 111 | static void removeMonitors(const char* mntpnt) { | ||
88 | snprintf( monitor, sizeof( monitor ), "partitions%s/usedspace", mntpnt ); | 112 | snprintf( monitor, sizeof( monitor ), "partitions%s/usedspace", mntpnt ); | ||
89 | removeMonitor( monitor ); | 113 | removeMonitor( monitor ); | ||
90 | snprintf( monitor, sizeof( monitor ), "partitions%s/freespace", mntpnt ); | 114 | snprintf( monitor, sizeof( monitor ), "partitions%s/freespace", mntpnt ); | ||
91 | removeMonitor( monitor ); | 115 | removeMonitor( monitor ); | ||
92 | snprintf( monitor, sizeof( monitor ), "partitions%s/filllevel", mntpnt ); | 116 | snprintf( monitor, sizeof( monitor ), "partitions%s/filllevel", mntpnt ); | ||
93 | removeMonitor( monitor ); | 117 | removeMonitor( monitor ); | ||
118 | snprintf( monitor, sizeof( monitor ), "partitions%s/total", mntpnt ); | ||||
119 | removeMonitor( monitor ); | ||||
94 | } | 120 | } | ||
95 | 121 | | |||
96 | void initDiskStat( struct SensorModul* sm ) | 122 | void initDiskStat( struct SensorModul* sm ) | ||
97 | { | 123 | { | ||
98 | DiskInfo* disk_info; | 124 | DiskInfo* disk_info; | ||
99 | 125 | | |||
100 | DiskStatList = NULL; | 126 | DiskStatList = NULL; | ||
101 | OldDiskStatList = NULL; | 127 | OldDiskStatList = NULL; | ||
102 | DiskStatSM = sm; | 128 | DiskStatSM = sm; | ||
103 | if ( updateDiskStat() < 0 ) | 129 | if ( updateDiskStat() < 0 ) | ||
104 | return; | 130 | return; | ||
105 | 131 | | |||
106 | registerMonitor( "partitions/list", "listview", printDiskStat, printDiskStatInfo, sm ); | 132 | registerMonitor( "partitions/list", "listview", printDiskStat, printDiskStatInfo, sm ); | ||
107 | 133 | | |||
134 | registerMonitors( "/all" ); | ||||
135 | | ||||
108 | for ( disk_info = first_ctnr( DiskStatList ); disk_info; disk_info = next_ctnr( DiskStatList ) ) { | 136 | for ( disk_info = first_ctnr( DiskStatList ); disk_info; disk_info = next_ctnr( DiskStatList ) ) { | ||
109 | registerMonitors(disk_info->mntpnt); | 137 | registerMonitors(disk_info->mntpnt); | ||
110 | } | 138 | } | ||
111 | } | 139 | } | ||
112 | 140 | | |||
113 | void exitDiskStat( void ) | 141 | void exitDiskStat( void ) | ||
114 | { | 142 | { | ||
115 | DiskInfo* disk_info; | 143 | DiskInfo* disk_info; | ||
116 | 144 | | |||
117 | removeMonitor( "partitions/list" ); | 145 | removeMonitor( "partitions/list" ); | ||
118 | 146 | | |||
147 | removeMonitors( "/all" ); | ||||
148 | | ||||
119 | for ( disk_info = first_ctnr( DiskStatList ); disk_info; disk_info = next_ctnr( DiskStatList ) ) { | 149 | for ( disk_info = first_ctnr( DiskStatList ); disk_info; disk_info = next_ctnr( DiskStatList ) ) { | ||
120 | removeMonitors(disk_info->mntpnt); | 150 | removeMonitors(disk_info->mntpnt); | ||
121 | } | 151 | } | ||
122 | 152 | | |||
123 | destr_ctnr( DiskStatList, free ); | 153 | destr_ctnr( DiskStatList, free ); | ||
124 | if(OldDiskStatList) | 154 | if(OldDiskStatList) | ||
125 | destr_ctnr( OldDiskStatList, free ); | 155 | destr_ctnr( OldDiskStatList, free ); | ||
126 | } | 156 | } | ||
▲ Show 20 Lines • Show All 132 Lines • ▼ Show 20 Line(s) | 287 | { | |||
259 | output( "Device\tSize\tUsed\tAvailable\tUsed %%\tMount point\nM\tKB\tKB\tKB\t%%\ts\n" ); | 289 | output( "Device\tSize\tUsed\tAvailable\tUsed %%\tMount point\nM\tKB\tKB\tKB\t%%\ts\n" ); | ||
260 | } | 290 | } | ||
261 | 291 | | |||
262 | void printDiskStatUsed( const char* cmd ) | 292 | void printDiskStatUsed( const char* cmd ) | ||
263 | { | 293 | { | ||
264 | char *mntpnt = (char*)getMntPnt( cmd ); | 294 | char *mntpnt = (char*)getMntPnt( cmd ); | ||
265 | DiskInfo* disk_info; | 295 | DiskInfo* disk_info; | ||
266 | 296 | | |||
297 | unsigned long total = 0; | ||||
298 | int is_all = strcmp( mntpnt, "/all" ) == 0; | ||||
299 | | ||||
267 | for ( disk_info = first_ctnr( DiskStatList ); disk_info; disk_info = next_ctnr( DiskStatList ) ) { | 300 | for ( disk_info = first_ctnr( DiskStatList ); disk_info; disk_info = next_ctnr( DiskStatList ) ) { | ||
268 | if ( !strcmp( mntpnt, disk_info->mntpnt ) ) { | 301 | if ( !strcmp( mntpnt, disk_info->mntpnt ) || is_all ) { | ||
269 | unsigned long totalSizeKB = disk_info->statvfs.f_blocks * (disk_info->statvfs.f_frsize/1024); | 302 | unsigned long totalSizeKB = disk_info->statvfs.f_blocks * (disk_info->statvfs.f_frsize/1024); | ||
270 | 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 */ | 303 | 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 */ | ||
304 | | ||||
305 | if ( !is_all ) { | ||||
271 | output( "%ld\n", usedKB ); | 306 | output( "%ld\n", usedKB ); | ||
307 | } else { | ||||
308 | total += usedKB; | ||||
309 | } | ||||
310 | } | ||||
272 | } | 311 | } | ||
312 | | ||||
313 | if ( is_all ) { | ||||
314 | output( "%ld\n", total ); | ||||
273 | } | 315 | } | ||
274 | 316 | | |||
275 | output( "\n" ); | 317 | output( "\n" ); | ||
276 | } | 318 | } | ||
277 | 319 | | |||
278 | void printDiskStatUsedInfo( const char* cmd ) | 320 | void printDiskStatUsedInfo( const char* cmd ) | ||
279 | { | 321 | { | ||
280 | (void)cmd; | 322 | char *mntpnt = getMntPnt( cmd ); | ||
281 | output( "Used\t0\t0\tKB\n" ); | 323 | | ||
324 | output( "Used\t0\t%ld\tKB\n", getTotal( mntpnt ) ); | ||||
282 | } | 325 | } | ||
283 | 326 | | |||
284 | void printDiskStatFree( const char* cmd ) | 327 | void printDiskStatFree( const char* cmd ) | ||
285 | { | 328 | { | ||
286 | char *mntpnt = (char*)getMntPnt( cmd ); | 329 | char *mntpnt = (char*)getMntPnt( cmd ); | ||
287 | DiskInfo* disk_info; | 330 | DiskInfo* disk_info; | ||
288 | 331 | | |||
332 | unsigned long total = 0; | ||||
333 | int is_all = strcmp( mntpnt, "/all" ) == 0; | ||||
334 | | ||||
289 | for ( disk_info = first_ctnr( DiskStatList ); disk_info; disk_info = next_ctnr( DiskStatList ) ) { | 335 | for ( disk_info = first_ctnr( DiskStatList ); disk_info; disk_info = next_ctnr( DiskStatList ) ) { | ||
290 | if ( !strcmp( mntpnt, disk_info->mntpnt ) ) { | 336 | if ( !strcmp( mntpnt, disk_info->mntpnt ) || is_all ) { | ||
291 | 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 */ | 337 | 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 */ | ||
338 | if ( !is_all ) { | ||||
292 | output( "%ld\n", available ); | 339 | output( "%ld\n", available ); | ||
340 | } else { | ||||
341 | total += available; | ||||
342 | } | ||||
293 | } | 343 | } | ||
294 | } | 344 | } | ||
345 | | ||||
346 | if ( is_all ) { | ||||
347 | output( "%ld\n", total ); | ||||
348 | } | ||||
349 | | ||||
295 | output( "\n" ); | 350 | output( "\n" ); | ||
296 | } | 351 | } | ||
297 | 352 | | |||
298 | void printDiskStatFreeInfo( const char* cmd ) | 353 | void printDiskStatFreeInfo( const char* cmd ) | ||
299 | { | 354 | { | ||
300 | (void)cmd; | 355 | char *mntpnt = (char*)getMntPnt( cmd ); | ||
301 | output( "Available\t0\t0\tKB\n" ); | 356 | | ||
357 | output( "Available\t0\t%ld\tKB\n", getTotal( mntpnt ) ); | ||||
302 | } | 358 | } | ||
303 | 359 | | |||
304 | void printDiskStatPercent( const char* cmd ) | 360 | void printDiskStatPercent( const char* cmd ) | ||
305 | { | 361 | { | ||
306 | char *mntpnt = (char*)getMntPnt( cmd ); | 362 | char *mntpnt = (char*)getMntPnt( cmd ); | ||
307 | DiskInfo* disk_info; | 363 | DiskInfo* disk_info; | ||
308 | 364 | | |||
365 | unsigned long totalSize = 0; | ||||
366 | unsigned long totalAvailable = 0; | ||||
367 | int is_all = strcmp( mntpnt, "/all" ) == 0; | ||||
368 | | ||||
309 | for ( disk_info = first_ctnr( DiskStatList ); disk_info; disk_info = next_ctnr( DiskStatList ) ) { | 369 | for ( disk_info = first_ctnr( DiskStatList ); disk_info; disk_info = next_ctnr( DiskStatList ) ) { | ||
310 | if ( !strcmp( mntpnt, disk_info->mntpnt ) ) { | 370 | if ( !strcmp( mntpnt, disk_info->mntpnt ) || is_all ) { | ||
311 | unsigned long totalSizeKB = disk_info->statvfs.f_blocks * (disk_info->statvfs.f_frsize/1024); | 371 | unsigned long totalSizeKB = disk_info->statvfs.f_blocks * (disk_info->statvfs.f_frsize/1024); | ||
312 | 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 */ | 372 | 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 */ | ||
313 | 373 | | |||
374 | if ( is_all ) { | ||||
375 | totalSize += totalSizeKB; | ||||
376 | totalAvailable += available; | ||||
377 | } else { | ||||
314 | int percentageUsed = calculatePercentageUsed(totalSizeKB, available); | 378 | int percentageUsed = calculatePercentageUsed(totalSizeKB, available); | ||
315 | output( "%d\n", percentageUsed ); | 379 | output( "%d\n", percentageUsed ); | ||
316 | } | 380 | } | ||
317 | } | 381 | } | ||
382 | } | ||||
383 | | ||||
384 | if ( is_all ) { | ||||
385 | int percentageUsed = calculatePercentageUsed(totalSize, totalAvailable); | ||||
386 | output( "%d\n", percentageUsed ); | ||||
387 | } | ||||
318 | 388 | | |||
319 | output( "\n" ); | 389 | output( "\n" ); | ||
320 | } | 390 | } | ||
321 | 391 | | |||
322 | void printDiskStatPercentInfo( const char* cmd ) | 392 | void printDiskStatPercentInfo( const char* cmd ) | ||
323 | { | 393 | { | ||
324 | (void)cmd; | 394 | (void)cmd; | ||
325 | output( "Percentage Used\t0\t100\t%%\n" ); | 395 | output( "Percentage Used\t0\t100\t%%\n" ); | ||
326 | } | 396 | } | ||
397 | | ||||
398 | void printDiskStatTotal( const char* cmd ) | ||||
399 | { | ||||
400 | char *mntpnt = (char*)getMntPnt( cmd ); | ||||
401 | | ||||
402 | output( "%ld\n\n", getTotal( mntpnt ) ); | ||||
403 | } | ||||
404 | | ||||
405 | void printDiskStatTotalInfo( const char* cmd ) | ||||
406 | { | ||||
407 | (void)cmd; | ||||
408 | output( "Total Size\t0\t0\tKB\n" ); | ||||
409 | } |