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.5
Committed: Mon Mar 3 12:32:36 2003 UTC (21 years, 6 months ago) by tdb
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +0 -0 lines
State: FILE REMOVED
Log Message:
Following up on Pete's commit of the new ihost - the new configure stuff.
Also dropped the old libstatgrab.

File Contents

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