ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/disk_stats.c
Revision: 1.3
Committed: Thu Feb 20 13:19:52 2003 UTC (21 years, 2 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.2: +1 -2 lines
Log Message:
Removed references to ukcprog.h.
Fixed a missing * in the disk stats.
And removed a block of linux stuff that shouldn't have been there :-)
All with Pete's approval, of course.

File Contents

# User Rev Content
1 pajs 1.1 /*
2     * i-scream central monitoring system
3     * http://www.i-scream.org.uk
4     * Copyright (C) 2000-2002 i-scream
5     *
6     * This program is free software; you can redistribute it and/or
7     * modify it under the terms of the GNU General Public License
8     * as published by the Free Software Foundation; either version 2
9     * of the License, or (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19     */
20    
21     #ifdef HAVE_CONFIG_H
22     #include "config.h"
23     #endif
24    
25     #include "statgrab.h"
26     #include <stdlib.h>
27     #include <stdio.h>
28     #include <string.h>
29    
30     #ifdef SOLARIS
31     #include <sys/mnttab.h>
32     #include <sys/types.h>
33     #include <sys/statvfs.h>
34    
35     #define VALID_FS_TYPES {"ufs", "tmpfs"}
36    
37     #endif
38    
39     #define START_VAL 1
40    
41     char *copy_string(char *orig_ptr, const char *newtext){
42    
43     /* Maybe free if not NULL, and strdup rather than realloc and strcpy? */
44     orig_ptr=realloc(orig_ptr, (1+strlen(newtext)));
45     if(orig_ptr==NULL){
46     return NULL;
47     }
48     strcpy(orig_ptr, newtext);
49    
50     return orig_ptr;
51     }
52    
53    
54     void init_disk_stat(int start, int end, disk_stat_t *disk_stats){
55    
56     for(disk_stats+=start; start<=end; start++){
57     disk_stats->device_name=NULL;
58     disk_stats->fs_type=NULL;
59     disk_stats->mnt_point=NULL;
60    
61     disk_stats++;
62     }
63    
64     }
65    
66     disk_stat_t *get_disk_stats(int *entries){
67    
68     static disk_stat_t *disk_stats;
69     static int watermark=-1;
70    
71     char *fs_types[] = VALID_FS_TYPES;
72     int x, valid_type;
73    
74     int num_disks=0;
75     struct mnttab mp;
76     struct statvfs fs;
77     FILE *f;
78    
79     disk_stat_t *disk_ptr;
80    
81     if(watermark==-1){
82     disk_stats=malloc(START_VAL * sizeof(disk_stat_t));
83     if(disk_stats==NULL){
84     return NULL;
85     }
86     watermark=START_VAL;
87     init_disk_stat(0, watermark-1, disk_stats);
88     }
89    
90    
91     if ((f=fopen("/etc/mnttab", "r" ))==NULL){
92     return NULL;
93     }
94     while((getmntent(f, &mp)) == 0){
95     if ((statvfs(mp.mnt_mountp, &fs)) !=0){
96     continue;
97     }
98    
99     valid_type=0;
100     for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){
101     if(strcmp(mp.mnt_fstype, fs_types[x]) ==0){
102     valid_type=1;
103     break;
104     }
105     }
106    
107     if(valid_type){
108     if(num_disks>watermark-1){
109     disk_ptr=disk_stats;
110     if((disk_stats=realloc(disk_stats, (watermark*2 * sizeof(disk_stat_t))))==NULL){
111     disk_stats=disk_ptr;
112     return NULL;
113     }
114    
115     watermark=watermark*2;
116     init_disk_stat(num_disks, watermark-1, disk_stats);
117     }
118    
119     disk_ptr=disk_stats+num_disks;
120    
121     /* Memory leak in event of realloc failing */
122     /* Maybe make this char[bigenough] and do strncpy's and put a null in the end?
123     Downside is its a bit hungry for a lot of mounts, as MNT_MAX_SIZE woul prob be upwards
124     of a k each */
125     if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp.mnt_special))==NULL){
126     return NULL;
127     }
128    
129     if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp.mnt_fstype))==NULL){
130     return NULL;
131     }
132    
133     if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp.mnt_mountp))==NULL){
134     return NULL;
135     }
136    
137     disk_ptr->size = (long long)fs.f_frsize * (long long)fs.f_blocks;
138     disk_ptr->avail = (long long)fs.f_frsize * (long long)fs.f_bavail;
139     disk_ptr->used = (disk_ptr->size) - ((long long)fs.f_frsize * (long long)fs.f_bfree);
140    
141     disk_ptr->total_inodes=(long long)fs.f_files;
142     disk_ptr->used_inodes=disk_ptr->total_inodes - (long long)fs.f_ffree;
143     disk_ptr->free_inodes=(long long)fs.f_favail;
144    
145     num_disks++;
146     }
147     }
148    
149     *entries=num_disks;
150    
151     /* If this fails, there is very little i can do about it, so i'll ignore it :) */
152     fclose(f);
153    
154     return disk_stats;
155    
156     }
157 pajs 1.2
158 tdb 1.3 diskio_stat_t *get_diskio_stats(int *entries){
159 pajs 1.2
160     /* Do jibble */
161     return NULL;
162     }