~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to ksysguard/ksysguardd/NetBSD/diskstat.c

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    KSysGuard, the KDE System Guard
 
3
           
 
4
        Copyright (c) 2001 Tobias Koenig <tokoe@kde.org>
 
5
    
 
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.
 
9
 
 
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.
 
14
 
 
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.
 
18
 
 
19
*/
 
20
 
 
21
#include <config-workspace.h>
 
22
 
 
23
#include <stdio.h>
 
24
#include <stdlib.h>
 
25
#include <string.h>
 
26
#include <sys/param.h>
 
27
#include <sys/stat.h>
 
28
#include <sys/ucred.h>
 
29
#include <sys/mount.h>
 
30
#include <time.h>
 
31
#include <unistd.h>
 
32
 
 
33
#include "Command.h"
 
34
#include "ccont.h"
 
35
#include "diskstat.h"
 
36
#include "ksysguardd.h"
 
37
 
 
38
typedef struct {
 
39
        char device[256];
 
40
        char mntpnt[256];
 
41
        long blocks;
 
42
        long bfree;
 
43
        long bused;
 
44
        int bused_percent;
 
45
} DiskInfo;
 
46
 
 
47
static CONTAINER DiskStatList = 0;
 
48
static struct SensorModul* DiskStatSM;
 
49
 
 
50
char *getMntPnt(const char *cmd)
 
51
{
 
52
        static char device[1025];
 
53
        char *ptr;
 
54
 
 
55
        memset(device, 0, sizeof(device));
 
56
        sscanf(cmd, "partitions%1024s", device);
 
57
 
 
58
        ptr = (char *)rindex(device, '/');
 
59
        *ptr = '\0';
 
60
                
 
61
        return (char *)device;
 
62
}
 
63
 
 
64
int numMntPnt(void)
 
65
{
 
66
        struct statvfs *fs_info;
 
67
        int i, n, counter = 0;
 
68
 
 
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"))
 
72
                        counter++;
 
73
 
 
74
        return counter;
 
75
}
 
76
 
 
77
/* ------------------------------ public part --------------------------- */
 
78
 
 
79
void initDiskStat(struct SensorModul* sm)
 
80
{
 
81
        char monitor[1024];
 
82
        DiskInfo* disk_info;
 
83
 
 
84
        DiskStatList = new_ctnr();
 
85
        DiskStatSM = sm;
 
86
 
 
87
        updateDiskStat();
 
88
 
 
89
        registerMonitor("partitions/list", "listview", printDiskStat, printDiskStatInfo, DiskStatSM);
 
90
 
 
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);
 
98
        }
 
99
}
 
100
 
 
101
void checkDiskStat(void)
 
102
{
 
103
        if (numMntPnt() != level_ctnr(DiskStatList)) {
 
104
                /* a file system was mounted or unmounted
 
105
                   so we do a reset */
 
106
                exitDiskStat();
 
107
                initDiskStat(DiskStatSM);
 
108
        }
 
109
}
 
110
 
 
111
void exitDiskStat(void)
 
112
{
 
113
        DiskInfo *disk_info;
 
114
        char monitor[1024];
 
115
 
 
116
        removeMonitor("partitions/list");
 
117
 
 
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);
 
125
        }
 
126
 
 
127
        destr_ctnr(DiskStatList, free);
 
128
}
 
129
 
 
130
int updateDiskStat(void)
 
131
{
 
132
        struct statvfs *fs_info;
 
133
        struct statvfs fs;
 
134
        float percent;
 
135
        int i, mntcount;
 
136
        DiskInfo *disk_info;
 
137
 
 
138
        /* let's hope there is no difference between the DiskStatList and
 
139
           the number of mounted file systems */
 
140
        empty_ctnr(DiskStatList);
 
141
 
 
142
        mntcount = getmntinfo(&fs_info, MNT_WAIT);
 
143
 
 
144
        for (i = 0; i < mntcount; i++) {
 
145
                fs = fs_info[i];
 
146
                if (strcmp(fs.f_fstypename, "procfs") && strcmp(fs.f_fstypename, "devfs") && strcmp(fs.f_fstypename, "devfs")) {
 
147
                        percent = (((float)fs.f_blocks - (float)fs.f_bfree)/(float)fs.f_blocks);
 
148
                        percent = percent * 100;
 
149
                        if ((disk_info = (DiskInfo *)malloc(sizeof(DiskInfo))) == NULL) {
 
150
                                continue;
 
151
                        }
 
152
                        memset(disk_info, 0, sizeof(DiskInfo));
 
153
                        strlcpy(disk_info->device, fs.f_mntfromname, sizeof(disk_info->device));
 
154
                        if (!strcmp(fs.f_mntonname, "/")) {
 
155
                                strncpy(disk_info->mntpnt, "/root", 6);
 
156
                        } else {
 
157
                                strlcpy(disk_info->mntpnt, fs.f_mntonname, sizeof(disk_info->mntpnt));
 
158
                        }
 
159
                        disk_info->blocks = fs.f_blocks;
 
160
                        disk_info->bfree = fs.f_bfree;
 
161
                        disk_info->bused = (fs.f_blocks - fs.f_bfree);
 
162
                        disk_info->bused_percent = (int)percent;
 
163
 
 
164
                        push_ctnr(DiskStatList, disk_info);
 
165
                }
 
166
        }
 
167
        
 
168
        return 0;
 
169
}
 
170
 
 
171
void printDiskStat(const char* cmd)
 
172
{
 
173
        DiskInfo* disk_info;
 
174
        
 
175
        for (disk_info = first_ctnr(DiskStatList); disk_info; disk_info = next_ctnr(DiskStatList)) {
 
176
                fprintf(CurrentClient, "%s\t%ld\t%ld\t%ld\t%d\t%s\n",
 
177
                        disk_info->device,
 
178
                        disk_info->blocks,
 
179
                        disk_info->bused,
 
180
                        disk_info->bfree,
 
181
                        disk_info->bused_percent,
 
182
                        disk_info->mntpnt);
 
183
        }
 
184
 
 
185
        fprintf(CurrentClient, "\n");
 
186
}
 
187
 
 
188
void printDiskStatInfo(const char* cmd)
 
189
{
 
190
        fprintf(CurrentClient, "Device\tBlocks\tUsed\tAvailable\tUsed %%\tMountPoint\nM\tD\tD\tD\td\ts\n");
 
191
}
 
192
 
 
193
void printDiskStatUsed(const char* cmd)
 
194
{
 
195
        DiskInfo* disk_info;
 
196
        char *mntpnt = (char *)getMntPnt(cmd);
 
197
 
 
198
        for (disk_info = first_ctnr(DiskStatList); disk_info; disk_info = next_ctnr(DiskStatList)) {
 
199
                if (!strcmp(mntpnt, disk_info->mntpnt)) {
 
200
                        fprintf(CurrentClient, "%ld\n", disk_info->bused);
 
201
                }
 
202
        }
 
203
 
 
204
        fprintf(CurrentClient, "\n");
 
205
}
 
206
 
 
207
void printDiskStatUsedInfo(const char* cmd)
 
208
{
 
209
        fprintf(CurrentClient, "Used Blocks\t0\t-\tBlocks\n");
 
210
}
 
211
 
 
212
void printDiskStatFree(const char* cmd)
 
213
{
 
214
        DiskInfo* disk_info;
 
215
        char *mntpnt = (char *)getMntPnt(cmd);
 
216
 
 
217
        for (disk_info = first_ctnr(DiskStatList); disk_info; disk_info = next_ctnr(DiskStatList)) {
 
218
                if (!strcmp(mntpnt, disk_info->mntpnt)) {
 
219
                        fprintf(CurrentClient, "%ld\n", disk_info->bfree);
 
220
                }
 
221
        }
 
222
 
 
223
        fprintf(CurrentClient, "\n");
 
224
}
 
225
 
 
226
void printDiskStatFreeInfo(const char* cmd)
 
227
{
 
228
        fprintf(CurrentClient, "Free Blocks\t0\t-\tBlocks\n");
 
229
}
 
230
 
 
231
void printDiskStatPercent(const char* cmd)
 
232
{
 
233
        DiskInfo* disk_info;
 
234
        char *mntpnt = (char *)getMntPnt(cmd);
 
235
 
 
236
        for (disk_info = first_ctnr(DiskStatList); disk_info; disk_info = next_ctnr(DiskStatList)) {
 
237
                if (!strcmp(mntpnt, disk_info->mntpnt)) {
 
238
                        fprintf(CurrentClient, "%d\n", disk_info->bused_percent);
 
239
                }
 
240
        }
 
241
 
 
242
        fprintf(CurrentClient, "\n");
 
243
}
 
244
 
 
245
void printDiskStatPercentInfo(const char* cmd)
 
246
{
 
247
        fprintf(CurrentClient, "Used Blocks\t0\t100\t%%\n");
 
248
}