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.
26
#include <sys/param.h>
28
#include <sys/ucred.h>
29
#include <sys/mount.h>
36
#include "ksysguardd.h"
47
static CONTAINER DiskStatList = 0;
48
static struct SensorModul* DiskStatSM;
50
char *getMntPnt(const char *cmd)
52
static char device[1025];
55
memset(device, 0, sizeof(device));
56
sscanf(cmd, "partitions%1024s", device);
58
ptr = (char *)rindex(device, '/');
61
return (char *)device;
66
struct statfs *fs_info;
67
int i, n, counter = 0;
69
n = getmntinfo(&fs_info, MNT_WAIT);
70
for (i = 0; i < n; i++)
71
if (strcmp(fs_info[i].f_fstypename, "procfs") && strcmp(fs_info[i].f_fstypename, "swap") && strcmp(fs_info[i].f_fstypename, "devfs"))
77
/* ------------------------------ public part --------------------------- */
79
void initDiskStat(struct SensorModul* sm)
84
DiskStatList = new_ctnr();
89
registerMonitor("partitions/list", "listview", printDiskStat, printDiskStatInfo, DiskStatSM);
91
for (disk_info = first_ctnr(DiskStatList); disk_info; disk_info = next_ctnr(DiskStatList)) {
92
snprintf(monitor, sizeof(monitor), "partitions%s/usedspace", disk_info->mntpnt);
93
registerMonitor(monitor, "integer", printDiskStatUsed, printDiskStatUsedInfo, DiskStatSM);
94
snprintf(monitor, sizeof(monitor), "partitions%s/freespace", disk_info->mntpnt);
95
registerMonitor(monitor, "integer", printDiskStatFree, printDiskStatFreeInfo, DiskStatSM);
96
snprintf(monitor, sizeof(monitor), "partitions%s/filllevel", disk_info->mntpnt);
97
registerMonitor(monitor, "integer", printDiskStatPercent, printDiskStatPercentInfo, DiskStatSM);
101
void checkDiskStat(void)
103
if (numMntPnt() != level_ctnr(DiskStatList)) {
104
/* a filesystem was mounted or unmounted
107
initDiskStat(DiskStatSM);
111
void exitDiskStat(void)
116
removeMonitor("partitions/list");
118
for (disk_info = first_ctnr(DiskStatList); disk_info; disk_info = next_ctnr(DiskStatList)) {
119
snprintf(monitor, sizeof(monitor), "partitions%s/usedspace", disk_info->mntpnt);
120
removeMonitor(monitor);
121
snprintf(monitor, sizeof(monitor), "partitions%s/freespace", disk_info->mntpnt);
122
removeMonitor(monitor);
123
snprintf(monitor, sizeof(monitor), "partitions%s/filllevel", disk_info->mntpnt);
124
removeMonitor(monitor);
127
destr_ctnr(DiskStatList, free);
130
int updateDiskStat(void)
132
struct statfs *fs_info;
138
/* let's hope there is no difference between the DiskStatList and
139
the number of mounted filesystems */
140
for (i = level_ctnr(DiskStatList); i >= 0; --i)
141
free(pop_ctnr(DiskStatList));
143
mntcount = getmntinfo(&fs_info, MNT_WAIT);
145
for (i = 0; i < mntcount; i++) {
147
if (strcmp(fs.f_fstypename, "procfs") && strcmp(fs.f_fstypename, "devfs") && strcmp(fs.f_fstypename, "devfs")) {
148
percent = (((float)fs.f_blocks - (float)fs.f_bfree)/(float)fs.f_blocks);
149
percent = percent * 100;
150
if ((disk_info = (DiskInfo *)malloc(sizeof(DiskInfo))) == NULL) {
153
memset(disk_info, 0, sizeof(DiskInfo));
154
strlcpy(disk_info->device, fs.f_mntfromname, sizeof(disk_info->device));
155
if (!strcmp(fs.f_mntonname, "/")) {
156
strncpy(disk_info->mntpnt, "/root", 6);
158
strlcpy(disk_info->mntpnt, fs.f_mntonname, sizeof(disk_info->mntpnt));
160
disk_info->blocks = fs.f_blocks;
161
disk_info->bfree = fs.f_bfree;
162
disk_info->bused = (fs.f_blocks - fs.f_bfree);
163
disk_info->bused_percent = (int)percent;
165
push_ctnr(DiskStatList, disk_info);
172
void printDiskStat(const char* cmd)
176
for (disk_info = first_ctnr(DiskStatList); disk_info; disk_info = next_ctnr(DiskStatList)) {
177
fprintf(CurrentClient, "%s\t%ld\t%ld\t%ld\t%d\t%s\n",
182
disk_info->bused_percent,
186
fprintf(CurrentClient, "\n");
189
void printDiskStatInfo(const char* cmd)
191
fprintf(CurrentClient, "Device\tBlocks\tUsed\tAvailable\tUsed %%\tMountPoint\nM\tD\tD\tD\td\ts\n");
194
void printDiskStatUsed(const char* cmd)
197
char *mntpnt = (char *)getMntPnt(cmd);
199
for (disk_info = first_ctnr(DiskStatList); disk_info; disk_info = next_ctnr(DiskStatList)) {
200
if (!strcmp(mntpnt, disk_info->mntpnt)) {
201
fprintf(CurrentClient, "%ld\n", disk_info->bused);
205
fprintf(CurrentClient, "\n");
208
void printDiskStatUsedInfo(const char* cmd)
210
fprintf(CurrentClient, "Used Blocks\t0\t-\tBlocks\n");
213
void printDiskStatFree(const char* cmd)
216
char *mntpnt = (char *)getMntPnt(cmd);
218
for (disk_info = first_ctnr(DiskStatList); disk_info; disk_info = next_ctnr(DiskStatList)) {
219
if (!strcmp(mntpnt, disk_info->mntpnt)) {
220
fprintf(CurrentClient, "%ld\n", disk_info->bfree);
224
fprintf(CurrentClient, "\n");
227
void printDiskStatFreeInfo(const char* cmd)
229
fprintf(CurrentClient, "Free Blocks\t0\t-\tBlocks\n");
232
void printDiskStatPercent(const char* cmd)
235
char *mntpnt = (char *)getMntPnt(cmd);
237
for (disk_info = first_ctnr(DiskStatList); disk_info; disk_info = next_ctnr(DiskStatList)) {
238
if (!strcmp(mntpnt, disk_info->mntpnt)) {
239
fprintf(CurrentClient, "%d\n", disk_info->bused_percent);
243
fprintf(CurrentClient, "\n");
246
void printDiskStatPercentInfo(const char* cmd)
248
fprintf(CurrentClient, "Used Blocks\t0\t100\t%%\n");