1 |
/* |
2 |
* i-scream central monitoring system |
3 |
* Copyright (C) 2000-2002 i-scream |
4 |
* |
5 |
* This program is free software; you can redistribute it and/or |
6 |
* modify it under the terms of the GNU General Public License |
7 |
* as published by the Free Software Foundation; either version 2 |
8 |
* of the License, or (at your option) any later version. |
9 |
* |
10 |
* This program is distributed in the hope that it will be useful, |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
* GNU General Public License for more details. |
14 |
* |
15 |
* You should have received a copy of the GNU General Public License |
16 |
* along with this program; if not, write to the Free Software |
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 |
*/ |
19 |
|
20 |
#include <stdio.h> |
21 |
#include "ukcprog.h" |
22 |
#ifdef SOLARIS |
23 |
#include <sys/stat.h> |
24 |
#include <sys/swap.h> |
25 |
#include <unistd.h> |
26 |
#endif |
27 |
#ifdef LINUX |
28 |
#include <string.h> |
29 |
#endif |
30 |
#ifdef FREEBSD |
31 |
#include <sys/types.h> |
32 |
#include <pwd.h> |
33 |
#include <unistd.h> |
34 |
#include <fcntl.h> |
35 |
#include <kvm.h> |
36 |
#include <limits.h> |
37 |
#include <grp.h> |
38 |
#endif |
39 |
|
40 |
char *get_swap_stats(){ |
41 |
char *xml_swap_stats; |
42 |
long swaptotal=0; |
43 |
long swapfree=0; |
44 |
long swapused=0; |
45 |
#ifdef SOLARIS |
46 |
struct anoninfo ai; |
47 |
int pagesize; |
48 |
#endif |
49 |
#ifdef LINUX |
50 |
char *line; |
51 |
FILE *f; |
52 |
#endif |
53 |
#ifdef FREEBSD |
54 |
gid_t gid; |
55 |
struct group *kmem_grp; |
56 |
struct kvm_swap swapinfo; |
57 |
int pagesize; |
58 |
kvm_t *kvmd = NULL; |
59 |
char errbuf[_POSIX2_LINE_MAX]; |
60 |
#endif |
61 |
|
62 |
#ifdef SOLARIS |
63 |
if((pagesize=sysconf(_SC_PAGESIZE)) == -1){ |
64 |
errf("failed to get system pagesize (%m)"); |
65 |
return NULL; |
66 |
} |
67 |
if (swapctl(SC_AINFO, &ai) == -1) { |
68 |
errf("failed to get swap stats (%m)"); |
69 |
return NULL; |
70 |
} |
71 |
swaptotal=((ai.ani_max/1024)*pagesize)/1024; |
72 |
swapused=((ai.ani_resv/1024)*pagesize)/1024; |
73 |
swapfree=swaptotal-swapused; |
74 |
#endif |
75 |
#ifdef LINUX |
76 |
if ((f=fopen("/proc/meminfo", "r" ))==NULL) { |
77 |
errf("Failed to open memory stats (%m)"); |
78 |
return NULL; |
79 |
} |
80 |
while((line=fpgetline(f)) != NULL){ |
81 |
if (((strncmp(line,"Swap: ",6)) == 0)) { |
82 |
if((sscanf(line, "%*s %ld %ld %ld", &swaptotal, &swapused, &swapfree)) != 3){ |
83 |
errf("Failed to read swap details (%m)"); |
84 |
return NULL; |
85 |
} |
86 |
break; |
87 |
} |
88 |
} |
89 |
if ((fclose(f)) != 0) { |
90 |
errf("Failed to close file (%m)"); |
91 |
return NULL; |
92 |
} |
93 |
swaptotal=(swaptotal/1024)/1024; |
94 |
swapused=(swapused/1024)/1024; |
95 |
swapfree=(swapfree/1024)/1024; |
96 |
#endif |
97 |
#ifdef FREEBSD |
98 |
/* To do this, this has to be setgid kmem, or be run as root :( |
99 |
*/ |
100 |
#define MEM_GROUP "kmem" |
101 |
if((kmem_grp=getgrnam(MEM_GROUP))==NULL){ |
102 |
errf("Failed to find gid for kmem (%m)"); |
103 |
return NULL; |
104 |
} |
105 |
gid=getgid(); |
106 |
if((setegid(kmem_grp->gr_gid))!=0){ |
107 |
errf("Failed to become gid kmem (%m)\nCan not get swap info."); |
108 |
return NULL; |
109 |
} |
110 |
if((kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf)) == NULL){ |
111 |
errf("Failed to open kvm info to get swap stats (%m)"); |
112 |
return NULL; |
113 |
} |
114 |
if ((kvm_getswapinfo(kvmd, &swapinfo, 1,0)) == -1){ |
115 |
errf("Failed to get swap info (%m)"); |
116 |
return NULL; |
117 |
} |
118 |
if((kvm_close(kvmd))!=0){ |
119 |
errf("Failed to close kvm (%m)"); |
120 |
return NULL; |
121 |
} |
122 |
if((setegid(gid))!=0){ |
123 |
errf("Failed to lose gid kmem (%m)"); |
124 |
exit(1); |
125 |
return NULL; |
126 |
} |
127 |
pagesize=getpagesize(); |
128 |
swaptotal=((((long)swapinfo.ksw_total)*pagesize)/1024)/1024; |
129 |
swapused=((((long)swapinfo.ksw_used)*pagesize)/1024)/1024; |
130 |
swapfree=swaptotal-swapused; |
131 |
#endif |
132 |
|
133 |
|
134 |
|
135 |
if((xml_swap_stats=strf("<swap><total>%ld</total><used>%ld</used><free>%ld</free></swap>", swaptotal, swapused, swapfree)) == NULL){ |
136 |
errf("strf failed (%m)"); |
137 |
return NULL; |
138 |
} |
139 |
|
140 |
return xml_swap_stats; |
141 |
} |