File: diskstat.c

package info (click to toggle)
kde-workspace 4%3A4.11.13-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 67,756 kB
  • ctags: 47,705
  • sloc: cpp: 358,638; ansic: 34,695; xml: 5,231; perl: 1,598; sh: 1,307; ruby: 1,135; python: 651; asm: 566; makefile: 37
file content (322 lines) | stat: -rw-r--r-- 10,920 bytes parent folder | download | duplicates (3)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
    KSysGuard, the KDE System Guard

    Copyright (c) 2001 Tobias Koenig <tokoe@kde.org>

    This program is free software; you can redistribute it and/or
    modify it under the terms of version 2 of the GNU General Public
    License as published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/

#include <config-workspace.h>

#include <mntent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/statvfs.h>
#include <time.h>
#include <unistd.h>
#include <ctype.h>

#include "Command.h"
#include "ccont.h"
#include "diskstat.h"
#include "ksysguardd.h"

typedef struct {
    char device[ 256 ];
    char mntpnt[ 256 ];
    struct statvfs statvfs;
} DiskInfo;

static CONTAINER DiskStatList = 0;
static CONTAINER OldDiskStatList = 0;
static struct SensorModul* DiskStatSM;
char *getMntPnt( const char* cmd );

static void sanitize(char *str)  {
    if(str == NULL)
        return;
    while (*str != 0)  {
        if(*str == '\t' || *str == '\n' || *str == '\r' || *str == ' ' || !isascii(*str) )
            *str = '?';
        ++str;
    }
}

char *getMntPnt( const char* cmd )
{
    static char device[ 1025 ];
    char* ptr;

    memset( device, 0, sizeof( device ) );
    sscanf( cmd, "partitions%1024s", device );

    ptr = (char*)rindex( device, '/' );
    *ptr = '\0';

    return (char*)device;
}

/* ----------------------------- public part ------------------------------- */

static char monitor[ 1024 ];
static void registerMonitors(const char* mntpnt) {
    snprintf( monitor, sizeof( monitor ), "partitions%s/usedspace", mntpnt );
    registerMonitor( monitor, "integer", printDiskStatUsed, printDiskStatUsedInfo, DiskStatSM );
    snprintf( monitor, sizeof( monitor ), "partitions%s/freespace", mntpnt );
    registerMonitor( monitor, "integer", printDiskStatFree, printDiskStatFreeInfo, DiskStatSM );
    snprintf( monitor, sizeof( monitor ), "partitions%s/filllevel", mntpnt );
    registerMonitor( monitor, "integer", printDiskStatPercent, printDiskStatPercentInfo, DiskStatSM );
}
static void removeMonitors(const char* mntpnt) {
    snprintf( monitor, sizeof( monitor ), "partitions%s/usedspace", mntpnt );
    removeMonitor( monitor );
    snprintf( monitor, sizeof( monitor ), "partitions%s/freespace", mntpnt );
    removeMonitor( monitor );
    snprintf( monitor, sizeof( monitor ), "partitions%s/filllevel", mntpnt );
    removeMonitor( monitor );
}

void initDiskStat( struct SensorModul* sm )
{
    DiskInfo* disk_info;

    DiskStatList = NULL;
    OldDiskStatList = NULL;
    DiskStatSM = sm;
    if ( updateDiskStat() < 0 )
        return;

    registerMonitor( "partitions/list", "listview", printDiskStat, printDiskStatInfo, sm );

    for ( disk_info = first_ctnr( DiskStatList ); disk_info; disk_info = next_ctnr( DiskStatList ) ) {
        registerMonitors(disk_info->mntpnt);
    }
}

void exitDiskStat( void )
{
    DiskInfo* disk_info;

    removeMonitor( "partitions/list" );

    for ( disk_info = first_ctnr( DiskStatList ); disk_info; disk_info = next_ctnr( DiskStatList ) ) {
        removeMonitors(disk_info->mntpnt);
    }

    destr_ctnr( DiskStatList, free );
    if(OldDiskStatList)
        destr_ctnr( OldDiskStatList, free );
}

void checkDiskStat( void )
{
    updateDiskStat();
    DiskInfo* disk_info_new;
    DiskInfo* disk_info_old;
    int changed = 0;
    for ( disk_info_new = first_ctnr( DiskStatList ); disk_info_new; disk_info_new = next_ctnr( DiskStatList ) ) {
        int found = 0;
        for ( disk_info_old = first_ctnr( OldDiskStatList ); disk_info_old; disk_info_old = next_ctnr( OldDiskStatList ) ) {
            if(strcmp(disk_info_new->mntpnt, disk_info_old->mntpnt) == 0) {
                free( remove_ctnr( OldDiskStatList ) );
                found = 1;
                continue;
            }
        }
        if(!found) {
            /* register all the devices that did not exist before*/
            registerMonitors(disk_info_new->mntpnt);
            changed++;
        }
    }
    /*Now remove all the devices that do not exist anymore*/
    for ( disk_info_old = first_ctnr( OldDiskStatList ); disk_info_old; disk_info_old = next_ctnr( OldDiskStatList ) ) {
        removeMonitors(disk_info_old->mntpnt);
        changed++;
    }
    destr_ctnr( OldDiskStatList, free );
    OldDiskStatList = NULL;
    updateDiskStat();
    if(changed)
        print_error( "RECONFIGURE" ); /*Let ksysguard know that we've added a sensor*/
}

int updateDiskStat( void )
{
    DiskInfo *disk_info;
    FILE *fh;
    struct mntent *mnt_info;

    if ( ( fh = setmntent( "/etc/mtab", "r" ) ) == NULL ) {
        print_error( "Cannot open \'/etc/mtab\'!\n" );
        return -1;
    }
    if(OldDiskStatList == 0) {
        OldDiskStatList = DiskStatList;
        DiskStatList = new_ctnr();
    } else {
        empty_ctnr(DiskStatList);
    }

    while ( ( mnt_info = getmntent( fh ) ) != NULL ) {
        /*
         * An entry which device name doesn't start with a '/' is
         * either a dummy file system or a network file system.
         * Add special handling for smbfs and cifs as is done by
         * coreutils as well.
         */
        if ( (mnt_info->mnt_fsname[0] != '/') ||
             !strcmp( mnt_info->mnt_type, "smbfs" ) ||
             !strcmp( mnt_info->mnt_type, "cifs" ) ||
             !strcmp( mnt_info->mnt_type, "proc" ) ||
             !strcmp( mnt_info->mnt_type, "devfs" ) ||
             !strcmp( mnt_info->mnt_type, "usbfs" ) ||
             !strcmp( mnt_info->mnt_type, "sysfs" ) ||
             !strcmp( mnt_info->mnt_type, "tmpfs" ) ||
             !strcmp( mnt_info->mnt_type, "devpts" ) )
            continue;  /* Skip these file systems */

        if ( ( disk_info = (DiskInfo *)malloc( sizeof( DiskInfo ) ) ) == NULL )
            continue;

        memset( disk_info, 0, sizeof( DiskInfo ) );

        if ( statvfs( mnt_info->mnt_dir, &(disk_info->statvfs) ) < 0 )
            continue;

        strncpy( disk_info->device, mnt_info->mnt_fsname, sizeof( disk_info->device ) );
        disk_info->device[ sizeof(disk_info->device) -1] = 0;

        strncpy( disk_info->mntpnt, mnt_info->mnt_dir, sizeof( disk_info->mntpnt ) );
        disk_info->mntpnt[ sizeof(disk_info->mntpnt) - 1] = 0;
        sanitize(disk_info->mntpnt);

        push_ctnr( DiskStatList, disk_info );
    }
    endmntent( fh );

    return 0;
}

int calculatePercentageUsed( unsigned long totalSizeKB, unsigned long available) {
    if (!available)
        return 0;

    unsigned long totalSizeKBdividedBy100 = (50 + totalSizeKB )/ 100;
    if (!totalSizeKBdividedBy100)
        return 0;

    int percentageUsed = 100 - available / totalSizeKBdividedBy100; /* Percentage is 1 - available / totalSizeKB, meaning that we count root-only reserved space as "used" here */
    /* If we have rounded down to 0%, make it 1%, like "df" does */
    if (percentageUsed == 0)
        return 1;
    return percentageUsed;
}

void printDiskStat( const char* cmd )
{
    DiskInfo* disk_info;

    (void)cmd;
    for ( disk_info = first_ctnr( DiskStatList ); disk_info; disk_info = next_ctnr( DiskStatList ) ) {
        /* See man statvfs(2) for meaning of fields */
        unsigned long totalSizeKB =  disk_info->statvfs.f_blocks * (disk_info->statvfs.f_frsize/1024);
        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 */
        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 */
        int percentageUsed = calculatePercentageUsed(totalSizeKB, available);
        output( "%s\t%ld\t%ld\t%ld\t%d\t%s\n",
                disk_info->device,
                totalSizeKB,
                usedKB,
                available,
                percentageUsed,
                disk_info->mntpnt );
    }

    output( "\n" );
}

void printDiskStatInfo( const char* cmd )
{
    (void)cmd;
    output( "Device\tSize\tUsed\tAvailable\tUsed %%\tMount point\nM\tKB\tKB\tKB\t%%\ts\n" );
}

void printDiskStatUsed( const char* cmd )
{
    char *mntpnt = (char*)getMntPnt( cmd );
    DiskInfo* disk_info;

    for ( disk_info = first_ctnr( DiskStatList ); disk_info; disk_info = next_ctnr( DiskStatList ) ) {
        if ( !strcmp( mntpnt, disk_info->mntpnt ) ) {
            unsigned long totalSizeKB =  disk_info->statvfs.f_blocks * (disk_info->statvfs.f_frsize/1024);
            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 */
            output( "%ld\n", usedKB );
        }
    }

    output( "\n" );
}

void printDiskStatUsedInfo( const char* cmd )
{
    (void)cmd;
    output( "Used\t0\t0\tKB\n" );
}

void printDiskStatFree( const char* cmd )
{
    char *mntpnt = (char*)getMntPnt( cmd );
    DiskInfo* disk_info;

    for ( disk_info = first_ctnr( DiskStatList ); disk_info; disk_info = next_ctnr( DiskStatList ) ) {
        if ( !strcmp( mntpnt, disk_info->mntpnt ) ) {
            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 */
            output( "%ld\n", available );
        }
    }
    output( "\n" );
}

void printDiskStatFreeInfo( const char* cmd )
{
    (void)cmd;
    output( "Available\t0\t0\tKB\n" );
}

void printDiskStatPercent( const char* cmd )
{
    char *mntpnt = (char*)getMntPnt( cmd );
    DiskInfo* disk_info;

    for ( disk_info = first_ctnr( DiskStatList ); disk_info; disk_info = next_ctnr( DiskStatList ) ) {
        if ( !strcmp( mntpnt, disk_info->mntpnt ) ) {
            unsigned long totalSizeKB =  disk_info->statvfs.f_blocks * (disk_info->statvfs.f_frsize/1024);
            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 */

            int percentageUsed = calculatePercentageUsed(totalSizeKB, available);
            output( "%d\n", percentageUsed );
        }
    }

    output( "\n" );
}

void printDiskStatPercentInfo( const char* cmd )
{
    (void)cmd;
    output( "Percentage Used\t0\t100\t%%\n" );
}