File: diskstat.c

package info (click to toggle)
kde-workspace 4:4.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 (388 lines) | stat: -rw-r--r-- 12,442 bytes parent folder | download
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
    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 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.

    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 <kdefakes.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/ucred.h>
#include <sys/mount.h>
#include <time.h>
#include <unistd.h>

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

typedef struct {
    char device[256];
    char mntpnt[256];
    long blocks;
    long bfree;
    long bused;
    int bused_percent;
    long bsize;
    long files;
    long ffree;
    long fused;
    int fused_percent;
} DiskInfo;

#define BLK2KB(disk_info, prop)                        \
    (disk_info->prop * (disk_info->bsize / 1024))

#define MNTPNT_NAME(disk_info)                        \
    (strcmp(disk_info->mntpnt, "/root") ? disk_info->mntpnt : "/")

static CONTAINER DiskStatList = 0;
static struct SensorModul* DiskStatSM;

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;
}

int numMntPnt(void)
{
    struct statfs *fs_info;
    int i, n, counter = 0;

    n = getmntinfo(&fs_info, MNT_WAIT);
    for (i = 0; i < n; i++)
        if (strcmp(fs_info[i].f_fstypename, "procfs") && strcmp(fs_info[i].f_fstypename, "swap") && strcmp(fs_info[i].f_fstypename, "devfs"))
            counter++;

    return counter;
}

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

void initDiskStat(struct SensorModul* sm)
{
    char monitor[1024];
    DiskInfo* disk_info;

    DiskStatList = new_ctnr();
    DiskStatSM = sm;

    updateDiskStat();

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

    for (disk_info = first_ctnr(DiskStatList); disk_info; disk_info = next_ctnr(DiskStatList)) {
        snprintf(monitor, sizeof(monitor), "partitions%s/usedspace", disk_info->mntpnt);
        registerMonitor(monitor, "integer", printDiskStatUsed, printDiskStatUsedInfo, DiskStatSM);
        snprintf(monitor, sizeof(monitor), "partitions%s/freespace", disk_info->mntpnt);
        registerMonitor(monitor, "integer", printDiskStatFree, printDiskStatFreeInfo, DiskStatSM);
        snprintf(monitor, sizeof(monitor), "partitions%s/filllevel", disk_info->mntpnt);
        registerMonitor(monitor, "integer", printDiskStatPercent, printDiskStatPercentInfo, DiskStatSM);
        snprintf(monitor, sizeof(monitor), "partitions%s/usedinode", disk_info->mntpnt);
        registerMonitor(monitor, "integer", printDiskStatIUsed, printDiskStatIUsedInfo, DiskStatSM);
        snprintf(monitor, sizeof(monitor), "partitions%s/freeinode", disk_info->mntpnt);
        registerMonitor(monitor, "integer", printDiskStatIFree, printDiskStatIFreeInfo, DiskStatSM);
        snprintf(monitor, sizeof(monitor), "partitions%s/inodelevel", disk_info->mntpnt);
        registerMonitor(monitor, "integer", printDiskStatIPercent, printDiskStatIPercentInfo, DiskStatSM);
    }
}

void checkDiskStat(void)
{
    if (numMntPnt() != level_ctnr(DiskStatList)) {
        /* a file system was mounted or unmounted
           so we do a reset */
        exitDiskStat();
        initDiskStat(DiskStatSM);
    }
}

void exitDiskStat(void)
{
    DiskInfo *disk_info;
    char monitor[1024];

    removeMonitor("partitions/list");

    for (disk_info = first_ctnr(DiskStatList); disk_info; disk_info = next_ctnr(DiskStatList)) {
        snprintf(monitor, sizeof(monitor), "partitions%s/usedspace", disk_info->mntpnt);
        removeMonitor(monitor);
        snprintf(monitor, sizeof(monitor), "partitions%s/freespace", disk_info->mntpnt);
        removeMonitor(monitor);
        snprintf(monitor, sizeof(monitor), "partitions%s/filllevel", disk_info->mntpnt);
        removeMonitor(monitor);
        snprintf(monitor, sizeof(monitor), "partitions%s/usedinode", disk_info->mntpnt);
        removeMonitor(monitor);
        snprintf(monitor, sizeof(monitor), "partitions%s/freeinode", disk_info->mntpnt);
        removeMonitor(monitor);
        snprintf(monitor, sizeof(monitor), "partitions%s/inodelevel", disk_info->mntpnt);
        removeMonitor(monitor);
    }

    destr_ctnr(DiskStatList, free);
}

int updateDiskStat(void)
{
    struct statfs *fs_info;
    struct statfs fs;
    float percent, fpercent;
    int i, mntcount;
    DiskInfo *disk_info;

    /* let's hope there is no difference between the DiskStatList and
       the number of mounted file systems */
    empty_ctnr(DiskStatList);

    mntcount = getmntinfo(&fs_info, MNT_WAIT);

    for (i = 0; i < mntcount; i++) {
        fs = fs_info[i];
        if (strcmp(fs.f_fstypename, "procfs") && strcmp(fs.f_fstypename, "devfs") && strcmp(fs.f_fstypename, "linprocfs")) {
            if ( fs.f_blocks != 0 )
                percent = (((float)fs.f_blocks - (float)fs.f_bfree)*100.0/(float)fs.f_blocks);
            else
                percent = 0;
            if (fs.f_files != 0)
                fpercent = (((float)fs.f_files - (float)fs.f_ffree)*100.0/(float)fs.f_files);
            else
                fpercent = 0;
            if ((disk_info = (DiskInfo *)malloc(sizeof(DiskInfo))) == NULL)
                continue;
            memset(disk_info, 0, sizeof(DiskInfo));
            strlcpy(disk_info->device, fs.f_mntfromname, sizeof(disk_info->device));
            if (!strcmp(fs.f_mntonname, "/"))
                strncpy(disk_info->mntpnt, "/root", 6);
            else
                strlcpy(disk_info->mntpnt, fs.f_mntonname, sizeof(disk_info->mntpnt));
            disk_info->blocks = fs.f_blocks;
            disk_info->bfree = fs.f_bfree;
            disk_info->bused = (fs.f_blocks - fs.f_bfree);
            disk_info->bused_percent = (int)percent;
            disk_info->bsize = fs.f_bsize;
            push_ctnr(DiskStatList, disk_info);
            disk_info->files = fs.f_files;
            disk_info->ffree = fs.f_ffree;
            disk_info->fused = fs.f_files - fs.f_ffree;
            disk_info->fused_percent = (int)fpercent;
        }
    }

    return 0;
}

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

    for (disk_info = first_ctnr(DiskStatList); disk_info; disk_info = next_ctnr(DiskStatList)) {
        fprintf(CurrentClient, "%s\t%ld\t%ld\t%ld\t%d\t%ld\t%ld\t%ld\t%d\t%s\n",
            disk_info->device,
            BLK2KB(disk_info, blocks),
            BLK2KB(disk_info, bused),
            BLK2KB(disk_info, bfree),
            disk_info->bused_percent,
            disk_info->files,
            disk_info->fused,
            disk_info->ffree,
            disk_info->fused_percent,
            MNTPNT_NAME(disk_info));
    }

    fprintf(CurrentClient, "\n");
}

void printDiskStatInfo(const char* cmd)
{
    fprintf(CurrentClient, "Device\tCapacity\tUsed\tAvailable\tUsed %%\tInodes\tUsed Inodes\tFree Inodes\tInodes %%\tMountPoint\nM\tKB\tKB\tKB\t%%\td\td\td\t%%\ts\n");
}

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

    for (disk_info = first_ctnr(DiskStatList); disk_info; disk_info = next_ctnr(DiskStatList)) {
        if (!strcmp(mntpnt, disk_info->mntpnt)) {
            fprintf(CurrentClient, "%ld\n", BLK2KB(disk_info, bused));
            return;
        }
    }

    fprintf(CurrentClient, "\n");
}

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

    for (disk_info = first_ctnr(DiskStatList); disk_info; disk_info = next_ctnr(DiskStatList)) {
        if (!strcmp(mntpnt, disk_info->mntpnt)) {
            fprintf(CurrentClient, "Used Space (%s)\t0\t%ld\tKB\n", MNTPNT_NAME(disk_info), BLK2KB(disk_info, blocks));
            return;
        }
    }
    fprintf(CurrentClient, "Used Space (%s)\t0\t-\tKB\n", mntpnt);
}

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

    for (disk_info = first_ctnr(DiskStatList); disk_info; disk_info = next_ctnr(DiskStatList)) {
        if (!strcmp(mntpnt, disk_info->mntpnt)) {
            fprintf(CurrentClient, "%ld\n", BLK2KB(disk_info, bfree));
            return;
        }
    }

    fprintf(CurrentClient, "\n");
}

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

    for (disk_info = first_ctnr(DiskStatList); disk_info; disk_info = next_ctnr(DiskStatList)) {
        if (!strcmp(mntpnt, disk_info->mntpnt)) {
            fprintf(CurrentClient, "Free Space (%s)\t0\t%ld\tKB\n", MNTPNT_NAME(disk_info), BLK2KB(disk_info, blocks));
            return;
        }
    }
    fprintf(CurrentClient, "Free Space (%s)\t0\t-\tKB\n", mntpnt);
}

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

    for (disk_info = first_ctnr(DiskStatList); disk_info; disk_info = next_ctnr(DiskStatList)) {
        if (!strcmp(mntpnt, disk_info->mntpnt)) {
            fprintf(CurrentClient, "%d\n", disk_info->bused_percent);
            return;
        }
    }

    fprintf(CurrentClient, "\n");
}

void printDiskStatPercentInfo(const char* cmd)
{
    char *mntpnt = (char *)getMntPnt(cmd);

    fprintf(CurrentClient, "Used Space (%s)\t0\t100\t%%\n", mntpnt);
}

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

    for (disk_info = first_ctnr(DiskStatList); disk_info; disk_info = next_ctnr(DiskStatList)) {
        if (!strcmp(mntpnt, disk_info->mntpnt)) {
            fprintf(CurrentClient, "%ld\n", disk_info->fused);
            return;
        }
    }

    fprintf(CurrentClient, "\n");
}

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

    for (disk_info = first_ctnr(DiskStatList); disk_info; disk_info = next_ctnr(DiskStatList)) {
        if (!strcmp(mntpnt, disk_info->mntpnt)) {
            fprintf(CurrentClient, "Used Inodes (%s)\t0\t%ld\tKB\n", MNTPNT_NAME(disk_info), disk_info->files);
            return;
        }
    }
    fprintf(CurrentClient, "Used Inodes(%s)\t0\t-\tKB\n", mntpnt);
}

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

    for (disk_info = first_ctnr(DiskStatList); disk_info; disk_info = next_ctnr(DiskStatList)) {
        if (!strcmp(mntpnt, disk_info->mntpnt)) {
            fprintf(CurrentClient, "%ld\n", disk_info->ffree);
            return;
        }
    }

    fprintf(CurrentClient, "\n");
}

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

    for (disk_info = first_ctnr(DiskStatList); disk_info; disk_info = next_ctnr(DiskStatList)) {
        if (!strcmp(mntpnt, disk_info->mntpnt)) {
            fprintf(CurrentClient, "Free Inodes (%s)\t0\t%ld\tKB\n", MNTPNT_NAME(disk_info), disk_info->files);
            return;
        }
    }
    fprintf(CurrentClient, "Free Inodes (%s)\t0\t-\tKB\n", mntpnt);
}

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

    for (disk_info = first_ctnr(DiskStatList); disk_info; disk_info = next_ctnr(DiskStatList)) {
        if (!strcmp(mntpnt, disk_info->mntpnt)) {
            fprintf(CurrentClient, "%d\n", disk_info->fused_percent);
            return;
        }
    }

    fprintf(CurrentClient, "\n");
}

void printDiskStatIPercentInfo(const char* cmd)
{
    char *mntpnt = (char *)getMntPnt(cmd);

    fprintf(CurrentClient, "Used Inodes (%s)\t0\t100\t%%\n", mntpnt);
}