9ac3413d5fd562ff69cfa3ffa8ab938900ac2d9c
[monky] / src / netbsd.c
1 /* Conky, a system monitor, based on torsmo
2  *
3  * Any original torsmo code is licensed under the BSD license
4  *
5  * All code written since the fork of torsmo is licensed under the GPL
6  *
7  * Please see COPYING for details
8  *
9  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
11  *      (see AUTHORS)
12  * All rights reserved.
13  *
14  * This program is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  *
26  */
27
28 #include "netbsd.h"
29
30 static kvm_t *kd = NULL;
31 int kd_init = 0, nkd_init = 0;
32 u_int32_t sensvalue;
33 char errbuf[_POSIX2_LINE_MAX];
34
35 static int init_kvm(void)
36 {
37         if (kd_init) {
38                 return 0;
39         }
40
41         kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
42         if (kd == NULL) {
43                 warnx("cannot kvm_openfiles: %s", errbuf);
44                 return -1;
45         }
46         kd_init = 1;
47         return 0;
48 }
49
50 static int swapmode(int *retavail, int *retfree)
51 {
52         int n;
53         struct swapent *sep;
54
55         *retavail = 0;
56         *retfree = 0;
57
58         n = swapctl(SWAP_NSWAP, 0, 0);
59
60         if (n < 1) {
61                 warn("could not get swap information");
62                 return 0;
63         }
64
65         sep = (struct swapent *) malloc(n * (sizeof(*sep)));
66
67         if (sep == NULL) {
68                 warn("memory allocation failed");
69                 return 0;
70         }
71
72         if (swapctl(SWAP_STATS, (void *) sep, n) < n) {
73                 warn("could not get swap stats");
74                 return 0;
75         }
76         for (; n > 0; n--) {
77                 *retavail += (int) dbtob(sep[n - 1].se_nblks);
78                 *retfree += (int) dbtob(sep[n - 1].se_nblks - sep[n - 1].se_inuse);
79         }
80         *retavail = (int) (*retavail / 1024);
81         *retfree = (int) (*retfree / 1024);
82
83         return 1;
84 }
85
86 void prepare_update()
87 {
88 }
89
90 void update_uptime()
91 {
92         int mib[2] = { CTL_KERN, KERN_BOOTTIME };
93         struct timeval boottime;
94         time_t now;
95         int size = sizeof(boottime);
96
97         if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
98                         && (boottime.tv_sec != 0)) {
99                 time(&now);
100                 info.uptime = now - boottime.tv_sec;
101         } else {
102                 warn("could not get uptime");
103                 info.uptime = 0;
104         }
105 }
106
107 int check_mount(char *s)
108 {
109         /* stub */
110         return 0;
111 }
112
113 void update_meminfo()
114 {
115         int mib[] = { CTL_VM, VM_UVMEXP2 };
116         int total_pages, inactive_pages, free_pages;
117         int swap_avail, swap_free;
118         const int pagesize = getpagesize();
119         struct uvmexp_sysctl uvmexp;
120         size_t size = sizeof(uvmexp);
121
122         if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
123                 warn("could not get memory info");
124                 return;
125         }
126
127         total_pages = uvmexp.npages;
128         free_pages = uvmexp.free;
129         inactive_pages = uvmexp.inactive;
130
131         info.memmax = (total_pages * pagesize) >> 10;
132         info.mem = ((total_pages - free_pages - inactive_pages) * pagesize) >> 10;
133         info.memeasyfree = info.memfree = info.memmax - info.mem;
134
135         if (swapmode(&swap_avail, &swap_free) >= 0) {
136                 info.swapmax = swap_avail;
137                 info.swap = (swap_avail - swap_free);
138                 info.swapfree = swap_free;
139         }
140 }
141
142 void update_net_stats()
143 {
144         int i;
145         double delta;
146         struct ifnet ifnet;
147         struct ifnet_head ifhead;       /* interfaces are in a tail queue */
148         u_long ifnetaddr;
149         static struct nlist namelist[] = {
150                 { "_ifnet" },
151                 { NULL }
152         };
153         static kvm_t *nkd;
154
155         if (!nkd_init) {
156                 nkd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
157                 if (nkd == NULL) {
158                         warnx("cannot kvm_openfiles: %s", errbuf);
159                         warnx("maybe you need to setgid kmem this program?");
160                         return;
161                 } else if (kvm_nlist(nkd, namelist) != 0) {
162                         warn("cannot kvm_nlist");
163                         return;
164                 } else {
165                         nkd_init = 1;
166                 }
167         }
168
169         if (kvm_read(nkd, (u_long) namelist[0].n_value, (void *) &ifhead,
170                         sizeof(ifhead)) < 0) {
171                 warn("cannot kvm_read");
172                 return;
173         }
174
175         /* get delta */
176         delta = current_update_time - last_update_time;
177         if (delta <= 0.0001) {
178                 return;
179         }
180
181         for (i = 0, ifnetaddr = (u_long) ifhead.tqh_first;
182                         ifnet.if_list.tqe_next && i < 16;
183                         ifnetaddr = (u_long) ifnet.if_list.tqe_next, i++) {
184
185                 struct net_stat *ns;
186                 long long last_recv, last_trans;
187
188                 kvm_read(nkd, (u_long) ifnetaddr, (void *) &ifnet, sizeof(ifnet));
189                 ns = get_net_stat(ifnet.if_xname, NULL, NULL);
190                 ns->up = 1;
191                 last_recv = ns->recv;
192                 last_trans = ns->trans;
193
194                 if (ifnet.if_ibytes < ns->last_read_recv) {
195                         ns->recv += ((long long) 4294967295U - ns->last_read_recv) +
196                                 ifnet.if_ibytes;
197                 } else {
198                         ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
199                 }
200
201                 ns->last_read_recv = ifnet.if_ibytes;
202
203                 if (ifnet.if_obytes < ns->last_read_trans) {
204                         ns->trans += ((long long) 4294967295U - ns->last_read_trans) +
205                                 ifnet.if_obytes;
206                 } else {
207                         ns->trans += (ifnet.if_obytes - ns->last_read_trans);
208                 }
209
210                 ns->last_read_trans = ifnet.if_obytes;
211
212                 ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
213                 ns->last_read_recv = ifnet.if_ibytes;
214                 ns->trans += (ifnet.if_obytes - ns->last_read_trans);
215                 ns->last_read_trans = ifnet.if_obytes;
216
217                 ns->recv_speed = (ns->recv - last_recv) / delta;
218                 ns->trans_speed = (ns->trans - last_trans) / delta;
219         }
220 }
221
222 void update_total_processes()
223 {
224         /* It's easier to use kvm here than sysctl */
225
226         int n_processes;
227
228         info.procs = 0;
229
230         if (init_kvm() < 0) {
231                 return;
232         } else {
233                 kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
234                         &n_processes);
235         }
236
237         info.procs = n_processes;
238 }
239
240 void update_running_processes()
241 {
242         struct kinfo_proc2 *p;
243         int n_processes;
244         int i, cnt = 0;
245
246         info.run_procs = 0;
247
248         if (init_kvm() < 0) {
249                 return;
250         } else {
251                 p = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
252                         &n_processes);
253                 for (i = 0; i < n_processes; i++) {
254                         if (p[i].p_stat == LSRUN || p[i].p_stat == LSIDL
255                                         || p[i].p_stat == LSONPROC) {
256                                 cnt++;
257                         }
258                 }
259         }
260
261         info.run_procs = cnt;
262 }
263
264 struct cpu_load_struct {
265         unsigned long load[5];
266 };
267
268 struct cpu_load_struct fresh = {
269         {0, 0, 0, 0, 0}
270 };
271
272 long cpu_used, oldtotal, oldused;
273
274 void update_cpu_usage()
275 {
276         long used, total;
277         static u_int64_t cp_time[CPUSTATES];
278         size_t len = sizeof(cp_time);
279
280         info.cpu_usage = 0;
281
282         if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) {
283                 warn("cannot get kern.cp_time");
284         }
285
286         fresh.load[0] = cp_time[CP_USER];
287         fresh.load[1] = cp_time[CP_NICE];
288         fresh.load[2] = cp_time[CP_SYS];
289         fresh.load[3] = cp_time[CP_IDLE];
290         fresh.load[4] = cp_time[CP_IDLE];
291
292         used = fresh.load[0] + fresh.load[1] + fresh.load[2];
293         total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
294
295         if ((total - oldtotal) != 0) {
296                 info.cpu_usage = ((double) (used - oldused)) /
297                         (double) (total - oldtotal);
298         } else {
299                 info.cpu_usage = 0;
300         }
301
302         oldused = used;
303         oldtotal = total;
304 }
305
306 void update_load_average()
307 {
308         double v[3];
309
310         getloadavg(v, 3);
311
312         info.loadavg[0] = (float) v[0];
313         info.loadavg[1] = (float) v[1];
314         info.loadavg[2] = (float) v[2];
315 }
316
317 double get_acpi_temperature(int fd)
318 {
319         return -1;
320 }
321
322 void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
323 {
324 }
325
326 int open_acpi_temperature(const char *name)
327 {
328         return -1;
329 }
330
331 void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size)
332 {
333         if (!p_client_buffer || client_buffer_size <= 0) {
334                 return;
335         }
336
337         /* not implemented */
338         memset(p_client_buffer, 0, client_buffer_size);
339 }
340
341 /* char *get_acpi_fan() */
342 void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
343 {
344         if (!p_client_buffer || client_buffer_size <= 0) {
345                 return;
346         }
347
348         /* not implemented */
349         memset(p_client_buffer, 0, client_buffer_size);
350 }
351
352 void update_entropy(void)
353 {
354 }