added icons and xml to livewp directory in debian rules
[shermanaquarium] / sherman-aquarium / shermans / status_freebsd.c
1
2 /*
3
4 This piece of code is heavily based upon some code in timecop's <timecop@japan.co.jp> 
5 "BubbleMon  dockapp 1.2" The FreeBSD code there is orignally by oleg dashevskii 
6 <od@iclub.nsu.ru> and changed by Jonas Aaberg <cja@gmx.net to suit Sherman's aquarium.
7
8 Thanks goes out to bodnar istvan <bistvan@sliced.hu> for doing the FreeBSD testing for me!
9
10 */
11
12 #include <kvm.h>
13 #include <sys/dkstat.h>
14 #include <sys/vmmeter.h>
15 #include <sys/resource.h>
16 #include <sys/sysctl.h>
17 #include <vm/vm_param.h>
18
19 #include "defines.h"
20 #include "aquarium.h"
21 #include "status.h"
22
23
24 #define pagetob(size) ((size) << pageshift)
25
26 static kvm_t *kd = NULL;
27 static struct nlist nlst[] = {
28     {"_cp_time", 0},
29     {"_cnt", 0},
30     {"_bufspace", 0},
31     {0, 0}
32 };
33 static int pageshift;
34 static int previous_total=0, previous_load=0;
35
36 static u_int64_t oload = 0, ototal = 0;
37 static int firsttimes = 0, current = 0;
38 static int cpu_average_list[CPUSMOOTHNESS];
39
40
41 static int status_init_cpu_load_freebsd(void)
42 {
43     /* calculate page shift to convert pages into kilobytes */
44     int pagesize = getpagesize();
45     pageshift = 0;
46
47     while (pagesize > 1) {
48         pageshift++;
49         pagesize >>= 1;
50     }
51
52     /* open kernel memory */
53     kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open");
54
55     if (kd == NULL) {
56         puts("Could not open kernel virtual memory");
57         return 1;
58     }
59
60     kvm_nlist(kd, nlst);
61
62     if (nlst[0].n_type == 0 || nlst[1].n_type == 0 || nlst[2].n_type == 0) {
63         puts("Error extracting symbols");
64         return 2;
65     }
66
67     /* drop setgid & setuid (the latter should not be there really) */
68     seteuid(getuid());
69     setegid(getgid());
70
71     if (geteuid() != getuid() || getegid() != getgid()) {
72         puts("Unable to drop privileges");
73         return 3;
74     }
75
76     return 0;
77 }
78
79 void status_init(void)
80 {
81     status_init_cpu_load_freebsd();
82 }
83
84 void status_exit(void)
85 {
86     /* I guess it should actually CPU be closed, but I don't know how.*/
87 }
88
89 int status_cpu(void)
90 {
91
92 /* Returns the current CPU load in percent */
93
94     int loadPercentage;
95     int total, load;
96     unsigned long int cpu_time[CPUSTATES];
97     int i, cpuload;
98
99
100     if (firsttimes == 0) {
101         for (i = 0; i < CPUSMOOTHNESS; i++)
102             cpu_average_list[i] = 0;
103     }
104     /* Wait until we have CPUSMOOTHNESS messures */
105     if (firsttimes != CPUSMOOTHNESS)
106         firsttimes++;
107
108
109     if (kvm_read(kd, nlst[0].n_value, &cpu_time, sizeof(cpu_time))
110         != sizeof(cpu_time))
111         return 0;
112
113     load = cpu_time[CP_USER] + cpu_time[CP_SYS] + cpu_time[CP_NICE];
114     total = load + cpu_time[CP_IDLE];
115
116
117     if(total!=previous_total)
118         cpu_average_list[current] = (100 * (load - previous_load)) / (total - previous_total);
119     else
120         cpu_average_list[current] = (load - previous_load);
121         
122     current++;
123     if (current == CPUSMOOTHNESS)
124         current = 0;
125
126     previous_load = load;
127     previous_total = total;
128
129     if (firsttimes != CPUSMOOTHNESS)
130         return 0;
131
132     cpuload = 0;
133
134     for (i = 0; i < CPUSMOOTHNESS; i++)
135         cpuload += cpu_average_list[i];
136     return (cpuload / CPUSMOOTHNESS);
137
138 }
139
140 }
141
142 int status_sensors(int, int)
143 {
144     return 0;
145 }
146 int status_swap(void)
147 {
148     return 0;
149 }
150 int status_net(int type, int direction)
151 {
152     return 0;
153 }
154 int status_mem(void)
155 {
156     return 0;
157 }
158 int status_disc(char *drive)
159 {
160     return 0;
161 }
162