1 |
/* |
2 |
* i-scream central monitoring system |
3 |
* http://www.i-scream.org |
4 |
* Copyright (C) 2000-2003 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 |
#ifdef SOLARIS |
27 |
#include <sys/stat.h> |
28 |
#include <sys/swap.h> |
29 |
#include <unistd.h> |
30 |
#endif |
31 |
#ifdef LINUX |
32 |
#include <stdio.h> |
33 |
#include "tools.h" |
34 |
#endif |
35 |
#ifdef FREEBSD |
36 |
#include <unistd.h> |
37 |
#include <sys/types.h> |
38 |
#include <fcntl.h> |
39 |
#include <kvm.h> |
40 |
#endif |
41 |
|
42 |
swap_stat_t *get_swap_stats(){ |
43 |
|
44 |
static swap_stat_t swap_stat; |
45 |
|
46 |
#ifdef SOLARIS |
47 |
struct anoninfo ai; |
48 |
int pagesize; |
49 |
#endif |
50 |
#ifdef LINUX |
51 |
FILE *f; |
52 |
char *line_ptr; |
53 |
#endif |
54 |
#ifdef FREEBSD |
55 |
struct kvm_swap swapinfo; |
56 |
int pagesize; |
57 |
kvm_t *kvmd = NULL; |
58 |
#endif |
59 |
|
60 |
#ifdef SOLARIS |
61 |
if((pagesize=sysconf(_SC_PAGESIZE)) == -1){ |
62 |
return NULL; |
63 |
} |
64 |
if (swapctl(SC_AINFO, &ai) == -1) { |
65 |
return NULL; |
66 |
} |
67 |
swap_stat.total = (long long)ai.ani_max * (long long)pagesize; |
68 |
swap_stat.used = (long long)ai.ani_resv * (long long)pagesize; |
69 |
swap_stat.free = swap_stat.total - swap_stat.used; |
70 |
#endif |
71 |
#ifdef LINUX |
72 |
if ((f=fopen("/proc/meminfo", "r" ))==NULL) { |
73 |
return NULL; |
74 |
} |
75 |
if((line_ptr=f_read_line(f, "Swap:"))==NULL){ |
76 |
fclose(f); |
77 |
return NULL; |
78 |
} |
79 |
if((sscanf(line_ptr, "Swap: %lld %lld %lld", &swap_stat.total, &swap_stat.used, &swap_stat.free))!=3){ |
80 |
fclose(f); |
81 |
return NULL; |
82 |
} |
83 |
fclose(f); |
84 |
#endif |
85 |
#ifdef FREEBSD |
86 |
if((kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL)) == NULL){ |
87 |
return NULL; |
88 |
} |
89 |
if ((kvm_getswapinfo(kvmd, &swapinfo, 1,0)) == -1){ |
90 |
return NULL; |
91 |
} |
92 |
pagesize=getpagesize(); |
93 |
|
94 |
swap_stat.total= (long long)swapinfo.ksw_total * (long long)pagesize; |
95 |
swap_stat.used = (long long)swapinfo.ksw_used * (long long)pagesize; |
96 |
swap_stat.free = swap_stat.total-swap_stat.used; |
97 |
|
98 |
kvm_close(kvmd); |
99 |
#endif |
100 |
return &swap_stat; |
101 |
|
102 |
} |