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
|
/***************************************
$Header: /home/amb/CVS/procmeter3/modules/df.c,v 1.14 2009-04-21 17:26:58 amb Exp $
ProcMeter - A system monitoring program for Linux - Version 3.5b.
Disk capacity monitoring source file.
******************/ /******************
Written by Andrew M. Bishop
This file Copyright 1998-2009 Andrew M. Bishop
It may be distributed under the GNU Public License, version 2, or
any higher version. See section COPYING of the GNU Public license
for conditions under which this file may be redistributed.
***************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/vfs.h>
#include "procmeter.h"
/* The interface information. */
/*+ The template for the disk outputs +*/
ProcMeterOutput _outputs[2]=
{
/*+ The percentage used space +*/
{
/* char name[]; */ "DF_Used_%s",
/* char *description; */ "The percentage of the %s device mounted on %s that is occupied with files. "
"(This can exceed 100%% on UNIX format drives due to the reserved minimum free space.)",
/* char type; */ PROCMETER_GRAPH|PROCMETER_TEXT|PROCMETER_BAR,
/* short interval; */ 10,
/* char text_value[]; */ "unknown",
/* long graph_value; */ 0,
/* short graph_scale; */ 20,
/* char graph_units[]; */ "(%d%%)"
},
/*+ The amount of free space +*/
{
/* char name[]; */ "DF_Free_%s",
/* char *description; */ "The amount of space on the %s device mounted on %s that is available for non-root use. "
"(This can be negative on UNIX format drives since it does not include the reserved minimum free space.)",
/* char type; */ PROCMETER_TEXT,
/* short interval; */ 10,
/* char text_value[]; */ "0 MB",
/* long graph_value; */ -1,
/* short graph_scale; */ 0,
/* char graph_units[]; */ "n/a"
}
};
/*+ The outputs. +*/
ProcMeterOutput **outputs=NULL;
/*+ The module. +*/
ProcMeterModule module=
{
/* char name[]; */ "DiskUsage",
/* char *description; */ "The fraction of the disk that is occupied and the amount of space available."
};
/* The line buffer */
static char *line=NULL;
static size_t length=128;
/* The information about the disks */
static int ndisks=0;
static char **disk=NULL;
static int *mounted;
/* A function to add a disk */
static void add_disk(char *dev,char *mnt);
/*++++++++++++++++++++++++++++++++++++++
Load the module.
ProcMeterModule *Load Returns the module information.
++++++++++++++++++++++++++++++++++++++*/
ProcMeterModule *Load(void)
{
return(&module);
}
/*++++++++++++++++++++++++++++++++++++++
Initialise the module, creating the outputs as required.
ProcMeterOutput **Initialise Returns a NULL terminated list of outputs.
char *options The options string for the module from the .procmeterrc file.
++++++++++++++++++++++++++++++++++++++*/
ProcMeterOutput **Initialise(char *options)
{
FILE *f;
outputs=(ProcMeterOutput**)malloc(sizeof(ProcMeterOutput*));
outputs[0]=NULL;
/* Use the devices in /proc/mounts */
f=fopen("/proc/mounts","r");
if(!f)
fprintf(stderr,"ProcMeter(%s): Could not open '/proc/mounts'.\n",__FILE__);
else
{
if(!fgets_realloc(&line,&length,f))
fprintf(stderr,"ProcMeter(%s): Could not read '/proc/mounts'.\n",__FILE__);
else
do
{
char device[65],mount[65];
if(sscanf(line,"%64s %64s",device,mount)==2)
if(strcmp(mount,"none") && *mount=='/' && (*device=='/' || strstr(device, ":/")))
add_disk(device,mount);
}
while(fgets_realloc(&line,&length,f));
fclose(f);
}
/* Use the devices in /etc/fstab */
f=fopen("/etc/fstab","r");
if(!f)
fprintf(stderr,"ProcMeter(%s): Could not open '/etc/fstab'.\n",__FILE__);
else
{
if(!fgets_realloc(&line,&length,f))
fprintf(stderr,"ProcMeter(%s): Could not read '/etc/fstab'.\n",__FILE__);
else
do
{
char device[65],mount[65];
if(*line=='#')
continue;
if(sscanf(line,"%64s %64s",device,mount)==2)
if(strcmp(mount,"none") && *mount=='/' && (*device=='/' || strstr(device, ":/")))
add_disk(device,mount);
}
while(fgets_realloc(&line,&length,f));
fclose(f);
}
/* Get the other options */
if(options)
{
char *l=options;
while(*l && *l==' ')
l++;
while(*l)
{
char *r=l,pr;
while(*r && *r!=' ')
r++;
pr=*r;
*r=0;
add_disk("(unknown device)",l);
*r=pr;
while(*r && *r==' ')
r++;
if(!*r)
break;
l=r;
}
}
return(outputs);
}
/*++++++++++++++++++++++++++++++++++++++
Add a new disk to the list.
char *dev The name of the device to add.
char *mnt The mount point for the device.
++++++++++++++++++++++++++++++++++++++*/
static void add_disk(char *dev,char *mnt)
{
int i,j;
for(i=0;i<ndisks;i++)
if(!strcmp(disk[i],mnt))
return;
outputs=(ProcMeterOutput**)realloc((void*)outputs,(ndisks*2+3)*sizeof(ProcMeterOutput*));
for(i=0,j=ndisks*2;i<2;i++,j++)
{
outputs[j]=(ProcMeterOutput*)malloc(sizeof(ProcMeterOutput));
*outputs[j]=_outputs[i];
snprintf(outputs[j]->name,PROCMETER_NAME_LEN+1,_outputs[i].name,mnt);
outputs[j]->description=(char*)malloc(strlen(dev)+strlen(mnt)+strlen(_outputs[i].description)+4);
sprintf(outputs[j]->description,_outputs[i].description,dev,mnt);
}
disk=(char**)realloc((void*)disk,(ndisks+1)*sizeof(char*));
mounted=(int*)realloc((void*)mounted,(ndisks+1)*sizeof(int));
disk[ndisks]=(char*)malloc(strlen(mnt)+1);
strcpy(disk[ndisks],mnt);
ndisks++;
outputs[j]=NULL;
}
/*++++++++++++++++++++++++++++++++++++++
Perform an update on one of the statistics.
int Update Returns 0 if OK, else -1.
time_t now The current time.
ProcMeterOutput *output The output that the value is wanted for.
++++++++++++++++++++++++++++++++++++++*/
int Update(time_t now,ProcMeterOutput *output)
{
static time_t last=0;
int i;
/* Get the mounted disks from /proc/mounts */
if(now!=last)
{
FILE *f;
for(i=0;i<ndisks;i++)
mounted[i]=0;
f=fopen("/proc/mounts","r");
if(!f)
return(-1);
else
{
if(!fgets_realloc(&line,&length,f))
return(-1);
else
do
{
char device[65],mount[65];
if(sscanf(line,"%64s %64s",device,mount)==2)
if(strcmp(mount,"none") && *mount=='/' && (*device=='/' || strstr(device, ":/")))
for(i=0;i<ndisks;i++)
if(!strcmp(disk[i],mount))
mounted[i]=1;
}
while(fgets_realloc(&line,&length,f));
fclose(f);
}
last=now;
}
for(i=0;outputs[i];i++)
if(output==outputs[i])
{
struct statfs buf;
if(!mounted[i/2])
{
output->graph_value=0;
strcpy(output->text_value,"not found");
}
else if(statfs(disk[i/2],&buf))
{
output->graph_value=0;
strcpy(output->text_value,"statfs error");
}
else
{
if(i%2)
{
long long avail=(buf.f_bavail>>5)*(buf.f_bsize>>5);
sprintf(output->text_value,"%.1f MB",avail/1024.0);
}
else
{
double frac=100.0*(double)(buf.f_blocks-buf.f_bfree)/(double)(buf.f_blocks-buf.f_bfree+buf.f_bavail);
output->graph_value=PROCMETER_GRAPH_FLOATING(frac/output->graph_scale);
sprintf(output->text_value,"%.1f %%",frac);
}
}
return(0);
}
return(-1);
}
/*++++++++++++++++++++++++++++++++++++++
Tidy up and prepare to have the module unloaded.
++++++++++++++++++++++++++++++++++++++*/
void Unload(void)
{
int i;
if(outputs)
{
for(i=0;outputs[i];i++)
{
free(outputs[i]->description);
free(outputs[i]);
}
free(outputs);
}
if(ndisks)
{
for(i=0;i<ndisks;i++)
free(disk[i]);
free(disk);
free(mounted);
}
if(line)
free(line);
}
|