--- projects/cms/source/ihost/libstatgrab/disk_stat.c 2002/05/13 11:33:07 1.2 +++ projects/cms/source/ihost/libstatgrab/disk_stat.c 2002/05/19 20:54:10 1.9 @@ -1,6 +1,25 @@ +/* + * i-scream central monitoring system + * Copyright (C) 2000-2002 i-scream + * + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + #include #include -#include +#include "ukcprog.h" #include #ifdef SOLARIS @@ -35,15 +54,18 @@ typedef struct system_disks_t{ char *get_disk_stats(){ system_disks_t *sd=NULL; system_disks_t *sd_ptr=sd; - FILE *f; int counter=0; + char *xml_disk_stats; + char *xml_disk_stats_ptr; #ifdef SOLARIS struct mnttab mp; struct statvfs df; + FILE *f; #endif #ifdef LINUX struct mntent *mp; struct statfs df; + FILE *f; #endif #ifdef FREEBSD int nummnt; @@ -61,10 +83,22 @@ char *get_disk_stats(){ continue; } if((((strcmp(mp.mnt_fstype,"ufs"))==0) || (strcmp(mp.mnt_fstype,"tmpfs")) ==0)){ - sd_ptr=malloc(sizeof(system_disks_t)); - sd_ptr->disk=malloc(sizeof(disk_stat_t)); - sd_ptr->disk->device_name=strdup(mp.mnt_special); - sd_ptr->disk->mnt_point=strdup(mp.mnt_mountp); + if((sd_ptr=malloc(sizeof(system_disks_t))) == NULL){ + errf("malloc failed (%m)"); + return NULL; + } + if((sd_ptr->disk=malloc(sizeof(disk_stat_t))) == NULL){ + errf("malloc failed (%m)"); + return NULL; + } + if((sd_ptr->disk->device_name=strdup(mp.mnt_special)) == NULL) { + errf("strdup failed (%m)"); + return NULL; + } + if((sd_ptr->disk->mnt_point=strdup(mp.mnt_mountp)) == NULL){ + errf("strdup failed (%m)"); + return NULL; + } sd_ptr->disk->size=((df.f_frsize/1024) * df.f_blocks); sd_ptr->disk->used=(((df.f_frsize/1024) * df.f_blocks) -((df.f_frsize/1024) * df.f_bfree)); sd_ptr->disk->avail=(df.f_frsize/1024) * df.f_bavail; @@ -76,6 +110,11 @@ char *get_disk_stats(){ } + if((fclose(f))!=0){ + errf("Failed to close mnttab"); + return NULL; + } + #endif #ifdef linux if ((f=fopen("/etc/mtab", "r" ))==NULL){ @@ -90,10 +129,22 @@ char *get_disk_stats(){ } if((((strcmp(mp->mnt_type, MNTTYPE_NFS))==0) || (strcmp(mp->mnt_type,MNTTYPE_IGNORE)) ==0)) continue; - sd_ptr=malloc(sizeof(system_disks_t)); - sd_ptr->disk=malloc(sizeof(disk_stat_t)); - sd_ptr->disk->device_name=strdup(mp->mnt_fsname); - sd_ptr->disk->mnt_point=strdup(mp->mnt_dir); + if((sd_ptr=malloc(sizeof(system_disks_t))) == NULL){ + errf("malloc failed (%m)"); + return NULL; + } + if((sd_ptr->disk=malloc(sizeof(disk_stat_t))) == NULL){ + errf("malloc failed (%m)"); + return NULL; + } + if((sd_ptr->disk->device_name=strdup(mp->mnt_fsname)) == NULL){ + errf("strdup failed (%m)"); + return NULL; + } + if((sd_ptr->disk->mnt_point=strdup(mp->mnt_dir)) == NULL){ + errf("strdup failed (%m)"); + return NULL; + } sd_ptr->disk->size=((df.f_bsize/1024) * df.f_blocks); sd_ptr->disk->used=((df.f_bsize/1024) * df.f_blocks) -((df.f_bsize/1024) * df.f_bfree); sd_ptr->disk->avail=((df.f_bsize/1024) * df.f_bavail); @@ -102,6 +153,12 @@ char *get_disk_stats(){ sd_ptr->next_disk=sd; sd=sd_ptr; } + + if ((fclose(f)) != 0) { + errf("Failed to close file (%m)"); + return NULL; + } + #endif #ifdef FREEBSD nummnt=getmntinfo(&mp , MNT_LOCAL); @@ -110,11 +167,23 @@ char *get_disk_stats(){ return NULL; } - for(;counterdisk=malloc(sizeof(disk_stat_t)); - sd_ptr->disk->device_name=strdup(mp->f_mntfromname); - sd_ptr->disk->mnt_point=strdup(mp->f_mntonname); + for(counter=0;counterdisk=malloc(sizeof(disk_stat_t))) == NULL){ + errf("malloc failed (%m)"); + return NULL; + } + if((sd_ptr->disk->device_name=strdup(mp->f_mntfromname))==NULL){ + errf("strdup failed (%m)"); + return NULL; + } + if((sd_ptr->disk->mnt_point=strdup(mp->f_mntonname))==NULL){ + errf("strdup failed (%m)"); + return NULL; + } sd_ptr->disk->size=((mp->f_bsize/1024) * mp->f_blocks); sd_ptr->disk->used=((mp->f_bsize/1024) * mp->f_blocks) -((mp->f_bsize/1024) * mp->f_bfree); sd_ptr->disk->avail=((mp->f_bsize/1024) * mp->f_bavail); @@ -122,29 +191,38 @@ char *get_disk_stats(){ sd_ptr->disk->f_inodes=mp->f_ffree; sd_ptr->next_disk=sd; sd=sd_ptr; + mp++; } #endif sd_ptr=sd; - while(sd_ptr!=NULL){ - printf("%s\n", sd_ptr->disk->device_name); - printf("%s\n", sd_ptr->disk->mnt_point); - printf("%ld\n", sd_ptr->disk->size); - printf("%ld\n", sd_ptr->disk->used); - printf("%ld\n", sd_ptr->disk->avail); - + xml_disk_stats=strdup(""); + + for(counter=0;sd_ptr!=NULL;counter++){ + xml_disk_stats_ptr=xml_disk_stats; + if((xml_disk_stats=strf("%s", xml_disk_stats, counter, sd_ptr->disk->device_name, sd_ptr->disk->mnt_point, sd_ptr->disk->size, sd_ptr->disk->used, sd_ptr->disk->avail, sd_ptr->disk->t_inodes, sd_ptr->disk->f_inodes, counter)) == NULL){ + errf("strf failed (%m)"); + return NULL; + } + free(xml_disk_stats_ptr); sd_ptr=sd_ptr->next_disk; - counter++; } + xml_disk_stats_ptr=xml_disk_stats; + xml_disk_stats=strf("%s",xml_disk_stats); + free(xml_disk_stats_ptr); - return "jibble"; + /* Cleaning up */ + sd_ptr=sd; + while(sd_ptr!=NULL){ + sd=sd_ptr->next_disk; + free(sd_ptr->disk->device_name); + free(sd_ptr->disk->mnt_point); + free(sd_ptr->disk); + free(sd_ptr); + sd_ptr=sd; + } + + return xml_disk_stats; } -int main(){ - printf("%s",get_disk_stats()); - exit(0); -} - - -