1 |
tdb |
1.4 |
/* |
2 |
|
|
* i-scream central monitoring system |
3 |
tdb |
1.6 |
* http://www.i-scream.org.uk |
4 |
tdb |
1.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 |
pajs |
1.1 |
#include <sys/utsname.h> |
22 |
pajs |
1.5 |
#include "ukcprog.h" |
23 |
pajs |
1.1 |
#include <stdio.h> |
24 |
|
|
#ifdef SOLARIS |
25 |
|
|
#include <kstat.h> |
26 |
|
|
#include <time.h> |
27 |
|
|
#endif |
28 |
|
|
#ifdef FREEBSD |
29 |
|
|
#include <sys/types.h> |
30 |
|
|
#include <sys/sysctl.h> |
31 |
|
|
#include <time.h> |
32 |
pajs |
1.2 |
#include <sys/time.h> |
33 |
pajs |
1.1 |
#endif |
34 |
|
|
|
35 |
|
|
char *get_os_info(){ |
36 |
|
|
char *xml_os_info; |
37 |
|
|
struct utsname os; |
38 |
|
|
long uptime; |
39 |
|
|
#ifdef SOLARIS |
40 |
|
|
time_t boottime,curtime; |
41 |
|
|
kstat_ctl_t *kc; |
42 |
|
|
kstat_t *ksp; |
43 |
|
|
kstat_named_t *kn; |
44 |
|
|
#endif |
45 |
|
|
#ifdef LINUX |
46 |
|
|
FILE *f; |
47 |
|
|
#endif |
48 |
|
|
#ifdef FREEBSD |
49 |
|
|
struct timeval boottime; |
50 |
|
|
time_t curtime; |
51 |
pajs |
1.2 |
size_t size; |
52 |
pajs |
1.1 |
#endif |
53 |
|
|
if((uname(&os)) < 0){ |
54 |
|
|
errf("Failed to get os stats (%m)"); |
55 |
|
|
return NULL; |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
/* Get the uptime */ |
59 |
|
|
#ifdef SOLARIS |
60 |
|
|
if ((kc = kstat_open()) == NULL) { |
61 |
|
|
errf("kstat_open failure (%m)"); |
62 |
|
|
return NULL; |
63 |
|
|
} |
64 |
|
|
if((ksp=kstat_lookup(kc, "unix", -1, "system_misc"))==NULL){ |
65 |
|
|
errf("failed to find lookup information (%m)"); |
66 |
|
|
return NULL; |
67 |
|
|
} |
68 |
|
|
if (kstat_read(kc, ksp, 0) == -1) { |
69 |
|
|
errf("Failed to read kernel information (%m)"); |
70 |
|
|
return NULL; |
71 |
|
|
} |
72 |
|
|
if((kn=kstat_data_lookup(ksp, "boot_time")) == NULL){ |
73 |
|
|
errf("Failed to get uptime (%m)"); |
74 |
|
|
return NULL; |
75 |
|
|
} |
76 |
|
|
boottime=(kn->value.ui32); |
77 |
|
|
time(&curtime); |
78 |
|
|
if((kstat_close(kc)) != 0){ |
79 |
|
|
errf("Failed to close kstat control structure (%m)"); |
80 |
|
|
return NULL; |
81 |
|
|
} |
82 |
|
|
uptime=curtime-boottime; |
83 |
|
|
#endif |
84 |
|
|
#ifdef LINUX |
85 |
|
|
if ((f=fopen("/proc/uptime", "r")) == NULL) { |
86 |
|
|
errf("Failed to open /proc/uptime (%m)"); |
87 |
|
|
return NULL; |
88 |
|
|
} |
89 |
|
|
if((fscanf(f,"%ld",&uptime)) != 1){ |
90 |
|
|
errf("Failed to read uptime (%m)"); |
91 |
|
|
return NULL; |
92 |
|
|
} |
93 |
|
|
if ((fclose(f)) != 0) { |
94 |
|
|
errf("Failed to close file (%m)"); |
95 |
|
|
return NULL; |
96 |
|
|
} |
97 |
|
|
#endif |
98 |
|
|
#ifdef FREEBSD |
99 |
|
|
#define BOOT_TIME_NAME "kern.boottime" |
100 |
|
|
|
101 |
|
|
if (sysctlbyname(BOOT_TIME_NAME, NULL, &size, NULL, NULL) < 0){ |
102 |
|
|
errf("sysctlbyname failed (%m)"); |
103 |
|
|
return NULL; |
104 |
|
|
} |
105 |
|
|
if (sysctlbyname(BOOT_TIME_NAME, &boottime, &size, NULL, NULL) < 0){ |
106 |
|
|
errf("sysctlbyname failed (%m)"); |
107 |
|
|
return NULL; |
108 |
|
|
} |
109 |
|
|
time(&curtime); |
110 |
|
|
uptime=curtime-boottime.tv_sec; |
111 |
|
|
|
112 |
|
|
#endif |
113 |
|
|
|
114 |
|
|
|
115 |
tdb |
1.3 |
xml_os_info=strf("<os><name>%s</name><release>%s</release><version>%s</version><sysname>%s</sysname><platform>%s</platform><uptime>%ld</uptime></os>", os.sysname, os.release, os.version, os.nodename, os.machine, uptime); |
116 |
pajs |
1.1 |
|
117 |
|
|
return xml_os_info; |
118 |
|
|
} |