ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/ihost/libstatgrab/swap_stats.c
Revision: 1.3
Committed: Tue May 21 16:47:12 2002 UTC (22 years, 6 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.2: +1 -0 lines
Log Message:
Added URL to GPL headers.

File Contents

# User Rev Content
1 tdb 1.2 /*
2     * i-scream central monitoring system
3 tdb 1.3 * http://www.i-scream.org.uk
4 tdb 1.2 * 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 pajs 1.1 #include <stdio.h>
22     #include "ukcprog.h"
23     #ifdef SOLARIS
24     #include <sys/stat.h>
25     #include <sys/swap.h>
26     #include <unistd.h>
27     #endif
28     #ifdef LINUX
29     #include <string.h>
30     #endif
31     #ifdef FREEBSD
32     #include <sys/types.h>
33     #include <pwd.h>
34     #include <unistd.h>
35     #include <fcntl.h>
36     #include <kvm.h>
37     #include <limits.h>
38     #include <grp.h>
39     #endif
40    
41     char *get_swap_stats(){
42     char *xml_swap_stats;
43     long swaptotal=0;
44     long swapfree=0;
45     long swapused=0;
46     #ifdef SOLARIS
47     struct anoninfo ai;
48     int pagesize;
49     #endif
50     #ifdef LINUX
51     char *line;
52     FILE *f;
53     #endif
54     #ifdef FREEBSD
55     gid_t gid;
56     struct group *kmem_grp;
57     struct kvm_swap swapinfo;
58     int pagesize;
59     kvm_t *kvmd = NULL;
60     char errbuf[_POSIX2_LINE_MAX];
61     #endif
62    
63     #ifdef SOLARIS
64     if((pagesize=sysconf(_SC_PAGESIZE)) == -1){
65     errf("failed to get system pagesize (%m)");
66     return NULL;
67     }
68     if (swapctl(SC_AINFO, &ai) == -1) {
69     errf("failed to get swap stats (%m)");
70     return NULL;
71     }
72     swaptotal=((ai.ani_max/1024)*pagesize)/1024;
73     swapused=((ai.ani_resv/1024)*pagesize)/1024;
74     swapfree=swaptotal-swapused;
75     #endif
76     #ifdef LINUX
77     if ((f=fopen("/proc/meminfo", "r" ))==NULL) {
78     errf("Failed to open memory stats (%m)");
79     return NULL;
80     }
81     while((line=fpgetline(f)) != NULL){
82     if (((strncmp(line,"Swap: ",6)) == 0)) {
83     if((sscanf(line, "%*s %ld %ld %ld", &swaptotal, &swapused, &swapfree)) != 3){
84     errf("Failed to read swap details (%m)");
85     return NULL;
86     }
87     break;
88     }
89     }
90     if ((fclose(f)) != 0) {
91     errf("Failed to close file (%m)");
92     return NULL;
93     }
94     swaptotal=(swaptotal/1024)/1024;
95     swapused=(swapused/1024)/1024;
96     swapfree=(swapfree/1024)/1024;
97     #endif
98     #ifdef FREEBSD
99     /* To do this, this has to be setgid kmem, or be run as root :(
100     */
101     #define MEM_GROUP "kmem"
102     if((kmem_grp=getgrnam(MEM_GROUP))==NULL){
103     errf("Failed to find gid for kmem (%m)");
104     return NULL;
105     }
106     gid=getgid();
107     if((setegid(kmem_grp->gr_gid))!=0){
108     errf("Failed to become gid kmem (%m)\nCan not get swap info.");
109     return NULL;
110     }
111     if((kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf)) == NULL){
112     errf("Failed to open kvm info to get swap stats (%m)");
113     return NULL;
114     }
115     if ((kvm_getswapinfo(kvmd, &swapinfo, 1,0)) == -1){
116     errf("Failed to get swap info (%m)");
117     return NULL;
118     }
119     if((kvm_close(kvmd))!=0){
120     errf("Failed to close kvm (%m)");
121     return NULL;
122     }
123     if((setegid(gid))!=0){
124     errf("Failed to lose gid kmem (%m)");
125     exit(1);
126     return NULL;
127     }
128     pagesize=getpagesize();
129     swaptotal=((((long)swapinfo.ksw_total)*pagesize)/1024)/1024;
130     swapused=((((long)swapinfo.ksw_used)*pagesize)/1024)/1024;
131     swapfree=swaptotal-swapused;
132     #endif
133    
134    
135    
136     if((xml_swap_stats=strf("<swap><total>%ld</total><used>%ld</used><free>%ld</free></swap>", swaptotal, swapused, swapfree)) == NULL){
137     errf("strf failed (%m)");
138     return NULL;
139     }
140    
141     return xml_swap_stats;
142     }