~ubuntu-branches/ubuntu/lucid/kdebase/lucid

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Ana Beatriz Guerrero Lopez
  • Date: 2009-04-05 05:22:13 UTC
  • mfrom: (0.4.2 experimental) (0.2.2 upstream)
  • mto: This revision was merged to the branch mainline in revision 235.
  • Revision ID: james.westby@ubuntu.com-20090405052213-39thr4l6p2ss07uj
Tags: 4:4.2.2-1
* New upstream release:
  - khtml fixes. (Closes: #290285, #359680)
  - Default konsole sessions can be deleted. (Closes: #286342)
  - Tag widget uses standard application palette. (Closes: #444800)
  - ... and surely many more but we have lost track...

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.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 statfs *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 filesystem 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 statfs *fs_info;
133
 
        struct statfs 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 filesystems */
140
 
        for (i = level_ctnr(DiskStatList); i >= 0; --i)
141
 
                free(pop_ctnr(DiskStatList));
142
 
 
143
 
        mntcount = getmntinfo(&fs_info, MNT_WAIT);
144
 
 
145
 
        for (i = 0; i < mntcount; i++) {
146
 
                fs = fs_info[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) {
151
 
                                continue;
152
 
                        }
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);
157
 
                        } else {
158
 
                                strlcpy(disk_info->mntpnt, fs.f_mntonname, sizeof(disk_info->mntpnt));
159
 
                        }
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;
164
 
 
165
 
                        push_ctnr(DiskStatList, disk_info);
166
 
                }
167
 
        }
168
 
        
169
 
        return 0;
170
 
}
171
 
 
172
 
void printDiskStat(const char* cmd)
173
 
{
174
 
        DiskInfo* disk_info;
175
 
        
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",
178
 
                        disk_info->device,
179
 
                        disk_info->blocks,
180
 
                        disk_info->bused,
181
 
                        disk_info->bfree,
182
 
                        disk_info->bused_percent,
183
 
                        disk_info->mntpnt);
184
 
        }
185
 
 
186
 
        fprintf(CurrentClient, "\n");
187
 
}
188
 
 
189
 
void printDiskStatInfo(const char* cmd)
190
 
{
191
 
        fprintf(CurrentClient, "Device\tBlocks\tUsed\tAvailable\tUsed %%\tMountPoint\nM\tD\tD\tD\td\ts\n");
192
 
}
193
 
 
194
 
void printDiskStatUsed(const char* cmd)
195
 
{
196
 
        DiskInfo* disk_info;
197
 
        char *mntpnt = (char *)getMntPnt(cmd);
198
 
 
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);
202
 
                }
203
 
        }
204
 
 
205
 
        fprintf(CurrentClient, "\n");
206
 
}
207
 
 
208
 
void printDiskStatUsedInfo(const char* cmd)
209
 
{
210
 
        fprintf(CurrentClient, "Used Blocks\t0\t-\tBlocks\n");
211
 
}
212
 
 
213
 
void printDiskStatFree(const char* cmd)
214
 
{
215
 
        DiskInfo* disk_info;
216
 
        char *mntpnt = (char *)getMntPnt(cmd);
217
 
 
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);
221
 
                }
222
 
        }
223
 
 
224
 
        fprintf(CurrentClient, "\n");
225
 
}
226
 
 
227
 
void printDiskStatFreeInfo(const char* cmd)
228
 
{
229
 
        fprintf(CurrentClient, "Free Blocks\t0\t-\tBlocks\n");
230
 
}
231
 
 
232
 
void printDiskStatPercent(const char* cmd)
233
 
{
234
 
        DiskInfo* disk_info;
235
 
        char *mntpnt = (char *)getMntPnt(cmd);
236
 
 
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);
240
 
                }
241
 
        }
242
 
 
243
 
        fprintf(CurrentClient, "\n");
244
 
}
245
 
 
246
 
void printDiskStatPercentInfo(const char* cmd)
247
 
{
248
 
        fprintf(CurrentClient, "Used Blocks\t0\t100\t%%\n");
249
 
}