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.1
Committed: Tue May 14 17:57:43 2002 UTC (22 years, 6 months ago) by pajs
Content type: text/plain
Branch: MAIN
Log Message:
gets swap stats for all systems.
needs to setgid kmem for this function to work on freebsd

File Contents

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