ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/ihost/libstatgrab/os_info.c
Revision: 1.4
Committed: Sat May 18 18:15:56 2002 UTC (22 years, 6 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.3: +19 -0 lines
Log Message:
i-scream is now licensed under the GPL. I've added the GPL headers to every
source file, and put a full copy of the license in the appropriate places.
I think I've covered everything. This is going to be a mad commit ;)

File Contents

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