summaryrefslogtreecommitdiff
path: root/misc/upclient/files/patch-stats-bsd.c
blob: b5aea3fa6c2ccfde81186a2392ab9f6028e69e7d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
--- src/stats-bsd.c.orig	Sat Jun 16 21:12:53 2001
+++ src/stats-bsd.c	Sun Nov 18 17:33:52 2001
@@ -26,37 +26,50 @@
  * @desc	Get statistics
  */
 void getstats(unsigned long *puptime, double *pload, int *pidle, char *os, char *oslevel, char *cpu) {
-  struct utsname uts;
-  struct timeval boottime;
-  time_t now;
   size_t size;
-  double loadavgs[2];
   int mib[2];
 
-  /* Get uptime */
-  time(&now);
+  {					/* ========== uptime ========== */
+    static struct timeval boottime;
+    static int initialized = 0;
+    time_t now;
+
+    if (!initialized) {
+      initialized = 1;
+
   mib[0] = CTL_KERN;
   mib[1] = KERN_BOOTTIME;
   size = sizeof(boottime);
-  if(sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && (boottime.tv_sec != 0)) {
+
+      if (sysctl (mib, 2, &boottime, &size, NULL, 0) == -1)
+	err (1, "sysctl: kern.boottime");
+    }
+
+    if (boottime.tv_sec) {
+      time_t now;
+
+      time (&now);
+
     *puptime = now - boottime.tv_sec;
     *puptime /= 60;
   }
+  }
+
+  if(cfg_sendload) {			/* ========== loadavg ========== */
+    double loadavgs[3] = { 0. };
 
-  if(cfg_sendload) {
-    /* Get load average */
     getloadavg(loadavgs, 3);
     /* Use the 3rd element (15 minute load average) */
     *pload = loadavgs[2];
   }
 
-  if(cfg_sendos) {
-    /* Get os info */
-    uname(&uts);
-  }
+  if(cfg_sendcpu) {			/* ========== cpu ========== */
+    static char buf [CPU_SIZE] = "";
+    static int initialized = 0;
+
+    if (!initialized) {
+      initialized = 1;
 
-  if(cfg_sendcpu) {
-    /* Get CPU info */
     mib[0] = CTL_HW;
     if(cfg_sendcpulevel == 1) {
       mib[1] = HW_MACHINE; /* Send architecture */
@@ -64,17 +77,35 @@
       mib[1] = HW_MODEL;   /* Send architecture's specific model */
     }
     size = CPU_SIZE;
-    sysctl(mib, 2, cpu, &size, NULL, 0);
+
+      if (sysctl (mib, 2, buf, &size, NULL, 0) == -1)
+	warn ("sysctl: hw.model");
+    }
+
+    strncpy (cpu, buf, CPU_SIZE);
+    cpu [CPU_SIZE - 1] = 0;
   }
 
   if(cfg_sendos) {
+    static struct utsname uts;
+    static int initialized = 0;
+
+    if (!initialized) {
+      initialized = 1;
+	    
+      uname(&uts);
+    }
+
     strncpy(os, uts.sysname, OS_SIZE - 1);
+    os [OS_SIZE - 1] = 0;
+
     if(cfg_sendoslevel) {
       strncpy(oslevel, uts.release, OSLEVEL_SIZE - 1);
+      oslevel [OSLEVEL_SIZE - 1] = 0;
     }
   }
 
-  if(cfg_sendidle) {
+  if(cfg_sendidle) {			/* ========== idle_time ========== */
     static kvm_t *kp;
     static int initialized = 0;
     static struct nlist namelist [] = {