1 /* Conky, a system monitor, based on torsmo
3 * Any original torsmo code is licensed under the BSD license
5 * All code written since the fork of torsmo is licensed under the GPL
7 * Please see COPYING for details
9 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10 * Copyright (c) 2007 Toni Spets
11 * Copyright (c) 2005-2007 Brenden Matthews, Philip Kovacs, et. al.
13 * All rights reserved.
15 * This program is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 #include <sys/types.h>
38 #include <sys/sysinfo.h>
40 #ifndef HAVE_CLOCK_GETTIME
45 // #include <assert.h>
49 #include <sys/ioctl.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <linux/sockios.h>
60 #define SHORTSTAT_TEMPL "%*s %llu %llu %llu"
61 #define LONGSTAT_TEMPL "%*s %llu %llu %llu "
63 static int show_nice_processes;
65 /* This flag tells the linux routines to use the /proc system where possible,
66 * even if other api's are available, e.g. sysinfo() or getloadavg().
67 * the reason for this is to allow for /proc-based distributed monitoring.
68 * using a flag in this manner creates less confusing code. */
69 static int prefer_proc = 0;
79 struct sysinfo s_info;
82 info.uptime = (double) s_info.uptime;
89 if (!(fp = open_file("/proc/uptime", &rep))) {
93 fscanf(fp, "%lf", &info.uptime);
96 info.mask |= (1 << INFO_UPTIME);
99 int check_mount(char *s)
102 FILE *mtab = fopen("/etc/mtab", "r");
105 char buf1[256], buf2[128];
107 while (fgets(buf1, 256, mtab)) {
108 sscanf(buf1, "%*s %128s", buf2);
109 if (!strcmp(s, buf2)) {
116 ERR("Could not open mtab");
121 /* these things are also in sysinfo except Buffers:
122 * (that's why I'm reading them from proc) */
124 void update_meminfo()
129 /* unsigned int a; */
132 info.mem = info.memmax = info.swap = info.swapmax = info.bufmem =
133 info.buffers = info.cached = 0;
135 if (!(meminfo_fp = open_file("/proc/meminfo", &rep))) {
139 while (!feof(meminfo_fp)) {
140 if (fgets(buf, 255, meminfo_fp) == NULL) {
144 if (strncmp(buf, "MemTotal:", 9) == 0) {
145 sscanf(buf, "%*s %Lu", &info.memmax);
146 } else if (strncmp(buf, "MemFree:", 8) == 0) {
147 sscanf(buf, "%*s %Lu", &info.mem);
148 } else if (strncmp(buf, "SwapTotal:", 10) == 0) {
149 sscanf(buf, "%*s %Lu", &info.swapmax);
150 } else if (strncmp(buf, "SwapFree:", 9) == 0) {
151 sscanf(buf, "%*s %Lu", &info.swap);
152 } else if (strncmp(buf, "Buffers:", 8) == 0) {
153 sscanf(buf, "%*s %Lu", &info.buffers);
154 } else if (strncmp(buf, "Cached:", 7) == 0) {
155 sscanf(buf, "%*s %Lu", &info.cached);
159 info.mem = info.memmax - info.mem;
160 info.swap = info.swapmax - info.swap;
162 info.bufmem = info.cached + info.buffers;
164 info.mask |= (1 << INFO_MEM) | (1 << INFO_BUFFERS);
169 inline void update_net_stats()
174 // FIXME: arbitrary size chosen to keep code simple.
176 unsigned int curtmp1, curtmp2;
183 // wireless info variables
184 int skfd, has_bitrate = 0;
185 struct wireless_info *winfo;
190 delta = current_update_time - last_update_time;
191 if (delta <= 0.0001) {
195 /* open file and ignore first two lines */
196 if (!(net_dev_fp = open_file("/proc/net/dev", &rep))) {
201 fgets(buf, 255, net_dev_fp); /* garbage */
202 fgets(buf, 255, net_dev_fp); /* garbage (field names) */
204 /* read each interface */
205 for (i2 = 0; i2 < 16; i2++) {
208 long long r, t, last_recv, last_trans;
210 if (fgets(buf, 255, net_dev_fp) == NULL) {
214 while (isspace((int) *p)) {
220 while (*p && *p != ':') {
229 ns = get_net_stat(s);
231 memset(&(ns->addr.sa_data), 0, 14);
232 last_recv = ns->recv;
233 last_trans = ns->trans;
235 /* bytes packets errs drop fifo frame compressed multicast|bytes ... */
236 sscanf(p, "%Ld %*d %*d %*d %*d %*d %*d %*d %Ld",
239 /* if recv or trans is less than last time, an overflow happened */
240 if (r < ns->last_read_recv) {
243 ns->recv += (r - ns->last_read_recv);
245 ns->last_read_recv = r;
247 if (t < ns->last_read_trans) {
250 ns->trans += (t - ns->last_read_trans);
252 ns->last_read_trans = t;
254 /*** ip addr patch ***/
255 i = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
257 conf.ifc_buf = malloc(sizeof(struct ifreq) * 16);
259 conf.ifc_len = sizeof(struct ifreq) * 16;
261 ioctl((long) i, SIOCGIFCONF, &conf);
263 for (k = 0; k < conf.ifc_len / sizeof(struct ifreq); k++) {
267 ((struct ifreq *) conf.ifc_buf)[k].ifr_ifrn.ifrn_name);
268 ns->addr = ((struct ifreq *) conf.ifc_buf)[k].ifr_ifru.ifru_addr;
275 /*** end ip addr patch ***/
277 /* calculate speeds */
278 ns->net_rec[0] = (ns->recv - last_recv) / delta;
279 ns->net_trans[0] = (ns->trans - last_trans) / delta;
283 for (i = 0; (unsigned) i < info.net_avg_samples; i++) {
284 curtmp1 += ns->net_rec[i];
285 curtmp2 += ns->net_trans[i];
293 ns->recv_speed = curtmp1 / (double) info.net_avg_samples;
294 ns->trans_speed = curtmp2 / (double) info.net_avg_samples;
295 if (info.net_avg_samples > 1) {
296 for (i = info.net_avg_samples; i > 1; i--) {
297 ns->net_rec[i - 1] = ns->net_rec[i - 2];
298 ns->net_trans[i - 1] = ns->net_trans[i - 2];
303 /* update wireless info */
304 winfo = malloc(sizeof(struct wireless_info));
305 memset(winfo, 0, sizeof(struct wireless_info));
307 skfd = iw_sockets_open();
308 if (iw_get_basic_config(skfd, s, &(winfo->b)) > -1) {
310 // set present winfo variables
311 if (iw_get_stats(skfd, s, &(winfo->stats),
312 &winfo->range, winfo->has_range) >= 0) {
313 winfo->has_stats = 1;
315 if (iw_get_range_info(skfd, s, &(winfo->range)) >= 0) {
316 winfo->has_range = 1;
318 if (iw_get_ext(skfd, s, SIOCGIWAP, &wrq) >= 0) {
319 winfo->has_ap_addr = 1;
320 memcpy(&(winfo->ap_addr), &(wrq.u.ap_addr), sizeof(sockaddr));
324 if (iw_get_ext(skfd, s, SIOCGIWRATE, &wrq) >= 0) {
325 memcpy(&(winfo->bitrate), &(wrq.u.bitrate), sizeof(iwparam));
326 iw_print_bitrate(ns->bitrate, 16, winfo->bitrate.value);
331 if (winfo->has_range && winfo->has_stats
332 && ((winfo->stats.qual.level != 0)
333 || (winfo->stats.qual.updated & IW_QUAL_DBM))) {
334 if (!(winfo->stats.qual.updated & IW_QUAL_QUAL_INVALID)) {
335 ns->link_qual = winfo->stats.qual.qual;
336 ns->link_qual_max = winfo->range.max_qual.qual;
341 if (winfo->has_ap_addr) {
342 iw_sawap_ntop(&winfo->ap_addr, ns->ap);
346 if (winfo->b.has_essid) {
347 if (winfo->b.essid_on) {
348 snprintf(ns->essid, 32, "%s", winfo->b.essid);
350 snprintf(ns->essid, 32, "off/any");
354 snprintf(ns->mode, 16, "%s", iw_operation_mode[winfo->b.mode]);
356 iw_sockets_close(skfd);
363 info.mask |= (1 << INFO_NET);
368 void update_total_processes()
372 struct sysinfo s_info;
375 info.procs = s_info.procs;
382 if (!(fp = open_file("/proc/loadavg", &rep))) {
386 fscanf(fp, "%*f %*f %*f %*d/%hd", &info.procs);
389 info.mask |= (1 << INFO_PROCS);
392 #define CPU_SAMPLE_COUNT 15
394 unsigned long long cpu_user;
395 unsigned long long cpu_system;
396 unsigned long long cpu_nice;
397 unsigned long long cpu_idle;
398 unsigned long long cpu_iowait;
399 unsigned long long cpu_irq;
400 unsigned long long cpu_softirq;
401 unsigned long long cpu_steal;
402 unsigned long long cpu_total;
403 unsigned long long cpu_active_total;
404 unsigned long long cpu_last_total;
405 unsigned long long cpu_last_active_total;
406 double cpu_val[CPU_SAMPLE_COUNT];
408 static short cpu_setup = 0;
410 /* Determine if this kernel gives us "extended" statistics information in
412 * Kernels around 2.5 and earlier only reported user, system, nice, and
413 * idle values in proc stat.
414 * Kernels around 2.6 and greater report these PLUS iowait, irq, softirq,
416 void determine_longstat(char *buf)
418 unsigned long long iowait = 0;
420 KFLAG_SETOFF(KFLAG_IS_LONGSTAT);
421 /* scanf will either return -1 or 1 because there is only 1 assignment */
422 if (sscanf(buf, "%*s %*d %*d %*d %*d %llu", &iowait) > 0) {
423 KFLAG_SETON(KFLAG_IS_LONGSTAT);
432 if (info.cpu_usage) {
437 if (!(stat_fp = open_file("/proc/stat", &rep))) {
443 while (!feof(stat_fp)) {
444 if (fgets(buf, 255, stat_fp) == NULL) {
448 if (strncmp(buf, "cpu", 3) == 0 && isdigit(buf[3])) {
449 if (info.cpu_count == 0) {
450 determine_longstat(buf);
455 info.cpu_usage = malloc((info.cpu_count + 1) * sizeof(float));
460 #define TMPL_LONGSTAT "%*s %llu %llu %llu %llu %llu %llu %llu %llu"
461 #define TMPL_SHORTSTAT "%*s %llu %llu %llu %llu"
463 inline static void update_stat()
467 static struct cpu_info *cpu = NULL;
472 char *stat_template = NULL;
473 unsigned int malloc_cpu_size = 0;
475 /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
476 if (!cpu_setup || !info.cpu_usage) {
481 if (!stat_template) {
483 KFLAG_ISSET(KFLAG_IS_LONGSTAT) ? TMPL_LONGSTAT : TMPL_SHORTSTAT;
487 malloc_cpu_size = (info.cpu_count + 1) * sizeof(struct cpu_info);
488 cpu = malloc(malloc_cpu_size);
489 memset(cpu, 0, malloc_cpu_size);
492 if (!(stat_fp = open_file("/proc/stat", &rep))) {
494 if (info.cpu_usage) {
495 memset(info.cpu_usage, 0, info.cpu_count * sizeof(float));
501 while (!feof(stat_fp)) {
502 if (fgets(buf, 255, stat_fp) == NULL) {
506 if (strncmp(buf, "procs_running ", 14) == 0) {
507 sscanf(buf, "%*s %hu", &info.run_procs);
508 info.mask |= (1 << INFO_RUN_PROCS);
509 } else if (strncmp(buf, "cpu", 3) == 0) {
510 index = isdigit(buf[3]) ? ((int) buf[3]) - 0x2F : 0;
511 sscanf(buf, stat_template, &(cpu[index].cpu_user),
512 &(cpu[index].cpu_nice), &(cpu[index].cpu_system),
513 &(cpu[index].cpu_idle), &(cpu[index].cpu_iowait),
514 &(cpu[index].cpu_irq), &(cpu[index].cpu_softirq),
515 &(cpu[index].cpu_steal));
517 cpu[index].cpu_total = cpu[index].cpu_user + cpu[index].cpu_nice +
518 cpu[index].cpu_system + cpu[index].cpu_idle +
519 cpu[index].cpu_iowait + cpu[index].cpu_irq +
520 cpu[index].cpu_softirq + cpu[index].cpu_steal;
522 cpu[index].cpu_active_total = cpu[index].cpu_total -
523 (cpu[index].cpu_idle + cpu[index].cpu_iowait);
524 info.mask |= (1 << INFO_CPU);
526 double delta = current_update_time - last_update_time;
528 if (delta <= 0.001) {
532 cpu[index].cpu_val[0] = (cpu[index].cpu_active_total -
533 cpu[index].cpu_last_active_total) /
534 (float) (cpu[index].cpu_total - cpu[index].cpu_last_total);
536 for (i = 0; i < info.cpu_avg_samples; i++) {
537 curtmp += cpu[index].cpu_val[i];
539 /* TESTING -- I've removed this, because I don't think it is right.
540 * You shouldn't divide by the cpu count here ...
541 * removing for testing */
543 info.cpu_usage[index] = curtmp / info.cpu_avg_samples /
546 info.cpu_usage[index] = curtmp / info.cpu_avg_samples;
548 /* TESTING -- this line replaces the prev. "suspect" if/else */
549 info.cpu_usage[index] = curtmp / info.cpu_avg_samples;
551 cpu[index].cpu_last_total = cpu[index].cpu_total;
552 cpu[index].cpu_last_active_total = cpu[index].cpu_active_total;
553 for (i = info.cpu_avg_samples - 1; i > 0; i--) {
554 cpu[index].cpu_val[i] = cpu[index].cpu_val[i - 1];
561 void update_running_processes()
566 void update_cpu_usage()
571 void update_load_average()
573 #ifdef HAVE_GETLOADAVG
578 info.loadavg[0] = (float) v[0];
579 info.loadavg[1] = (float) v[1];
580 info.loadavg[2] = (float) v[2];
587 if (!(fp = open_file("/proc/loadavg", &rep))) {
588 info.loadavg[0] = info.loadavg[1] = info.loadavg[2] = 0.0;
591 fscanf(fp, "%f %f %f", &info.loadavg[0], &info.loadavg[1],
595 info.mask |= (1 << INFO_LOADAVG);
598 #define PROC_I8K "/proc/i8k"
599 #define I8K_DELIM " "
600 static char *i8k_procbuf = NULL;
606 i8k_procbuf = (char *) malloc(128 * sizeof(char));
608 if ((fp = fopen(PROC_I8K, "r")) == NULL) {
609 CRIT_ERR("/proc/i8k doesn't exist! use insmod to make sure the kernel "
610 "driver is loaded...");
613 memset(&i8k_procbuf[0], 0, 128);
614 if (fread(&i8k_procbuf[0], sizeof(char), 128, fp) == 0) {
615 ERR("something wrong with /proc/i8k...");
620 i8k.version = strtok(&i8k_procbuf[0], I8K_DELIM);
621 i8k.bios = strtok(NULL, I8K_DELIM);
622 i8k.serial = strtok(NULL, I8K_DELIM);
623 i8k.cpu_temp = strtok(NULL, I8K_DELIM);
624 i8k.left_fan_status = strtok(NULL, I8K_DELIM);
625 i8k.right_fan_status = strtok(NULL, I8K_DELIM);
626 i8k.left_fan_rpm = strtok(NULL, I8K_DELIM);
627 i8k.right_fan_rpm = strtok(NULL, I8K_DELIM);
628 i8k.ac_status = strtok(NULL, I8K_DELIM);
629 i8k.buttons_status = strtok(NULL, I8K_DELIM);
632 /***********************************************************/
633 /***********************************************************/
634 /***********************************************************/
636 static int no_dots(const struct dirent *d)
638 if (d->d_name[0] == '.') {
644 static int get_first_file_in_a_directory(const char *dir, char *s, int *rep)
646 struct dirent **namelist;
649 n = scandir(dir, &namelist, no_dots, alphasort);
652 ERR("scandir for %s: %s", dir, strerror(errno));
663 strncpy(s, namelist[0]->d_name, 255);
666 for (i = 0; i < n; i++) {
675 int open_sysfs_sensor(const char *dir, const char *dev, const char *type, int n,
676 int *div, char *devtype)
683 memset(buf, 0, sizeof(buf));
685 /* if device is NULL or *, get first */
686 if (dev == NULL || strcmp(dev, "*") == 0) {
689 if (!get_first_file_in_a_directory(dir, buf, &rep)) {
695 if (strcmp(dir, "/sys/class/hwmon/") == 0) {
697 /* buf holds result from get_first_file_in_a_directory() above,
698 * e.g. "hwmon0" -- append "/device" */
699 strcat(buf, "/device");
701 /* dev holds device number N as a string,
702 * e.g. "0", -- convert to "hwmon0/device" */
703 sprintf(buf, "hwmon%s/device", dev);
708 /* change vol to in */
709 if (strcmp(type, "vol") == 0) {
713 if (strcmp(type, "tempf") == 0) {
714 snprintf(path, 255, "%s%s/%s%d_input", dir, dev, "temp", n);
716 snprintf(path, 255, "%s%s/%s%d_input", dir, dev, type, n);
718 strncpy(devtype, path, 255);
721 fd = open(path, O_RDONLY);
723 CRIT_ERR("can't open '%s': %s\nplease check your device or remove this "
724 "var from Conky", path, strerror(errno));
727 if (strcmp(type, "in") == 0 || strcmp(type, "temp") == 0
728 || strcmp(type, "tempf") == 0) {
733 /* fan does not use *_div as a read divisor */
734 if (strcmp("fan", type) == 0) {
738 /* test if *_div file exist, open it and use it as divisor */
739 if (strcmp(type, "tempf") == 0) {
740 snprintf(path, 255, "%s%s/%s%d_div", dir, "one", "two", n);
742 snprintf(path, 255, "%s%s/%s%d_div", dir, dev, type, n);
745 divfd = open(path, O_RDONLY);
751 divn = read(divfd, divbuf, 63);
752 /* should read until n == 0 but I doubt that kernel will give these
753 * in multiple pieces. :) */
763 double get_sysfs_info(int *fd, int div, char *devtype, char *type)
771 lseek(*fd, 0, SEEK_SET);
778 n = read(*fd, buf, 63);
779 /* should read until n == 0 but I doubt that kernel will give these
780 * in multiple pieces. :) */
787 *fd = open(devtype, O_RDONLY);
789 ERR("can't open '%s': %s", devtype, strerror(errno));
792 /* My dirty hack for computing CPU value
793 * Filedil, from forums.gentoo.org */
794 /* if (strstr(devtype, "temp1_input") != NULL) {
795 return -15.096 + 1.4893 * (val / 1000.0);
798 /* divide voltage and temperature by 1000 */
799 /* or if any other divisor is given, use that */
800 if (strcmp(type, "tempf") == 0) {
802 return ((val / div + 40) * 9.0 / 5) - 40;
804 return ((val / 1000.0 + 40) * 9.0 / 5) - 40;
806 return ((val + 40) * 9.0 / 5) - 40;
819 /* Prior to kernel version 2.6.12, the CPU fan speed was available in
820 * ADT746X_FAN_OLD, whereas later kernel versions provide this information in
822 #define ADT746X_FAN "/sys/devices/temperatures/sensor1_fan_speed"
823 #define ADT746X_FAN_OLD "/sys/devices/temperatures/cpu_fan_speed"
825 void get_adt746x_fan(char *p_client_buffer, size_t client_buffer_size)
828 char adt746x_fan_state[64];
831 if (!p_client_buffer || client_buffer_size <= 0) {
835 if ((fp = open_file(ADT746X_FAN, &rep)) == NULL
836 && (fp = open_file(ADT746X_FAN_OLD, &rep)) == NULL) {
837 sprintf(adt746x_fan_state, "adt746x not found");
839 fgets(adt746x_fan_state, sizeof(adt746x_fan_state), fp);
840 adt746x_fan_state[strlen(adt746x_fan_state) - 1] = 0;
844 snprintf(p_client_buffer, client_buffer_size, "%s", adt746x_fan_state);
847 /* Prior to kernel version 2.6.12, the CPU temperature was found in
848 * ADT746X_CPU_OLD, whereas later kernel versions provide this information in
850 #define ADT746X_CPU "/sys/devices/temperatures/sensor1_temperature"
851 #define ADT746X_CPU_OLD "/sys/devices/temperatures/cpu_temperature"
853 void get_adt746x_cpu(char *p_client_buffer, size_t client_buffer_size)
856 char adt746x_cpu_state[64];
859 if (!p_client_buffer || client_buffer_size <= 0) {
863 if ((fp = open_file(ADT746X_CPU, &rep)) == NULL
864 && (fp = open_file(ADT746X_CPU_OLD, &rep)) == NULL) {
865 sprintf(adt746x_cpu_state, "adt746x not found");
867 fscanf(fp, "%2s", adt746x_cpu_state);
871 snprintf(p_client_buffer, client_buffer_size, "%s", adt746x_cpu_state);
874 /* Thanks to "Walt Nelson" <wnelsonjr@comcast.net> */
876 /***********************************************************************/
877 /* This file is part of x86info.
878 * (C) 2001 Dave Jones.
880 * Licensed under the terms of the GNU GPL License version 2.
882 * Estimate CPU MHz routine by Andrea Arcangeli <andrea@suse.de>
883 * Small changes by David Sterba <sterd9am@ss1000.ms.mff.cuni.cz> */
885 #if defined(__i386) || defined(__x86_64)
886 __inline__ unsigned long long int rdtsc()
888 unsigned long long int x;
890 __asm__ volatile(".byte 0x0f, 0x31":"=A" (x));
895 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
896 void get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size,
897 char *p_format, int divisor)
899 #if defined(__i386) || defined(__x86_64)
901 struct timeval tvstart, tvstop;
902 unsigned long long cycles[2]; /* gotta be 64 bit */
903 unsigned int microseconds; /* total time taken */
905 if (!p_client_buffer || client_buffer_size <= 0 || !p_format
910 memset(&tz, 0, sizeof(tz));
912 /* get this function in cached memory */
913 gettimeofday(&tvstart, &tz);
915 gettimeofday(&tvstart, &tz);
917 /* we don't trust that this is any specific length of time */
920 gettimeofday(&tvstop, &tz);
921 microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
922 (tvstop.tv_usec - tvstart.tv_usec);
924 snprintf(p_client_buffer, client_buffer_size, p_format,
925 (float) ((cycles[1] - cycles[0]) / microseconds) / divisor);
928 /* FIXME: hardwired: get freq for first cpu!
929 * this whole function needs to be rethought and redone for
930 * multi-cpu/multi-core/multi-threaded environments and
931 * arbitrary combinations thereof */
932 get_freq(p_client_buffer, client_buffer_size, p_format, divisor, 1);
937 #define CPUFREQ_PREFIX "/sys/devices/system/cpu"
938 #define CPUFREQ_POSTFIX "cpufreq/scaling_cur_freq"
940 /* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
941 char get_freq(char *p_client_buffer, size_t client_buffer_size, char *p_format,
942 int divisor, unsigned int cpu)
950 if (!p_client_buffer || client_buffer_size <= 0 || !p_format
956 char current_freq_file[128];
958 snprintf(current_freq_file, 127, "%s/cpu%d/%s", CPUFREQ_PREFIX, cpu - 1,
960 f = fopen(current_freq_file, "r");
962 /* if there's a cpufreq /sys node, read the current frequency from
963 * this node and divide by 1000 to get Mhz. */
964 if (fgets(s, sizeof(s), f)) {
965 s[strlen(s) - 1] = '\0';
966 freq = strtod(s, NULL);
969 snprintf(p_client_buffer, client_buffer_size, p_format,
970 (freq / 1000) / divisor);
975 // open the CPU information file
976 f = open_file("/proc/cpuinfo", &rep);
978 perror("Conky: Failed to access '/proc/cpuinfo' at get_freq()");
983 while (fgets(s, sizeof(s), f) != NULL) {
985 #if defined(__i386) || defined(__x86_64)
986 // and search for the cpu mhz
987 if (strncmp(s, "cpu MHz", 7) == 0 && cpu == 0) {
990 // different on alpha
991 if (strncmp(s, "cycle frequency [Hz]", 20) == 0 && cpu == 0) {
993 // this is different on ppc for some reason
994 if (strncmp(s, "clock", 5) == 0 && cpu == 0) {
995 #endif // defined(__alpha)
996 #endif // defined(__i386) || defined(__x86_64)
998 // copy just the number
999 strcpy(frequency, strchr(s, ':') + 2);
1000 #if defined(__alpha)
1002 frequency[strlen(frequency) - 6] = '\0';
1003 // kernel reports in Hz
1004 freq = strtod(frequency, NULL) / 1000000;
1007 frequency[strlen(frequency) - 1] = '\0';
1008 freq = strtod(frequency, NULL);
1012 if (strncmp(s, "processor", 9) == 0) {
1019 snprintf(p_client_buffer, client_buffer_size, p_format,
1020 (float) freq / divisor);
1024 #define CPUFREQ_VOLTAGE "cpufreq/scaling_voltages"
1026 /* /sys/devices/system/cpu/cpu0/cpufreq/scaling_voltages looks something
1036 * Peter Tarjan (ptarjan@citromail.hu) */
1038 /* return cpu voltage in mV (use divisor=1) or V (use divisor=1000) */
1039 char get_voltage(char *p_client_buffer, size_t client_buffer_size,
1040 char *p_format, int divisor, unsigned int cpu)
1046 char current_freq_file[128];
1049 /* build the voltage file name */
1051 snprintf(current_freq_file, 127, "%s/cpu%d/%s", CPUFREQ_PREFIX, cpu,
1054 if (!p_client_buffer || client_buffer_size <= 0 || !p_format
1059 /* read the current cpu frequency from the /sys node */
1060 f = fopen(current_freq_file, "r");
1062 if (fgets(s, sizeof(s), f)) {
1063 s[strlen(s) - 1] = '\0';
1064 freq = strtod(s, NULL);
1068 fprintf(stderr, "Conky: Failed to access '%s' at ", current_freq_file);
1069 perror("get_voltage()");
1076 snprintf(current_freq_file, 127, "%s/cpu%d/%s", CPUFREQ_PREFIX, cpu,
1079 /* use the current cpu frequency to find the corresponding voltage */
1080 f = fopen(current_freq_file, "r");
1086 if (fgets(line, 255, f) == NULL) {
1089 sscanf(line, "%d %d", &freq_comp, &voltage);
1090 if (freq_comp == freq) {
1096 fprintf(stderr, "Conky: Failed to access '%s' at ", current_freq_file);
1097 perror("get_voltage()");
1103 snprintf(p_client_buffer, client_buffer_size, p_format,
1104 (float) voltage / divisor);
1108 #define ACPI_FAN_DIR "/proc/acpi/fan/"
1110 void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
1117 if (!p_client_buffer || client_buffer_size <= 0) {
1121 /* yeah, slow... :/ */
1122 if (!get_first_file_in_a_directory(ACPI_FAN_DIR, buf, &rep)) {
1123 snprintf(p_client_buffer, client_buffer_size, "no fans?");
1127 snprintf(buf2, sizeof(buf2), "%s%s/state", ACPI_FAN_DIR, buf);
1129 fp = open_file(buf2, &rep);
1131 snprintf(p_client_buffer, client_buffer_size,
1132 "can't open fan's state file");
1135 memset(buf, 0, sizeof(buf));
1136 fscanf(fp, "%*s %99s", buf);
1139 snprintf(p_client_buffer, client_buffer_size, "%s", buf);
1142 #define ACPI_AC_ADAPTER_DIR "/proc/acpi/ac_adapter/"
1144 void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size)
1151 if (!p_client_buffer || client_buffer_size <= 0) {
1155 /* yeah, slow... :/ */
1156 if (!get_first_file_in_a_directory(ACPI_AC_ADAPTER_DIR, buf, &rep)) {
1157 snprintf(p_client_buffer, client_buffer_size, "no ac_adapters?");
1161 snprintf(buf2, sizeof(buf2), "%s%s/state", ACPI_AC_ADAPTER_DIR, buf);
1163 fp = open_file(buf2, &rep);
1165 snprintf(p_client_buffer, client_buffer_size,
1166 "No ac adapter found.... where is it?");
1169 memset(buf, 0, sizeof(buf));
1170 fscanf(fp, "%*s %99s", buf);
1173 snprintf(p_client_buffer, client_buffer_size, "%s", buf);
1177 /proc/acpi/thermal_zone/THRM/cooling_mode
1178 cooling mode: active
1179 /proc/acpi/thermal_zone/THRM/polling_frequency
1181 /proc/acpi/thermal_zone/THRM/state
1183 /proc/acpi/thermal_zone/THRM/temperature
1185 /proc/acpi/thermal_zone/THRM/trip_points
1187 passive: 73 C: tc1=4 tc2=3 tsp=40 devices=0xcdf6e6c0
1190 #define ACPI_THERMAL_DIR "/proc/acpi/thermal_zone/"
1191 #define ACPI_THERMAL_FORMAT "/proc/acpi/thermal_zone/%s/temperature"
1193 int open_acpi_temperature(const char *name)
1199 if (name == NULL || strcmp(name, "*") == 0) {
1202 if (!get_first_file_in_a_directory(ACPI_THERMAL_DIR, buf, &rep)) {
1208 snprintf(path, 255, ACPI_THERMAL_FORMAT, name);
1210 fd = open(path, O_RDONLY);
1212 ERR("can't open '%s': %s", path, strerror(errno));
1218 static double last_acpi_temp;
1219 static double last_acpi_temp_time;
1221 double get_acpi_temperature(int fd)
1227 /* don't update acpi temperature too often */
1228 if (current_update_time - last_acpi_temp_time < 11.32) {
1229 return last_acpi_temp;
1231 last_acpi_temp_time = current_update_time;
1233 /* seek to beginning */
1234 lseek(fd, 0, SEEK_SET);
1241 n = read(fd, buf, 255);
1243 ERR("can't read fd %d: %s", fd, strerror(errno));
1246 sscanf(buf, "temperature: %lf", &last_acpi_temp);
1250 return last_acpi_temp;
1254 hipo@lepakko hipo $ cat /proc/acpi/battery/BAT1/info
1256 design capacity: 4400 mAh
1257 last full capacity: 4064 mAh
1258 battery technology: rechargeable
1259 design voltage: 14800 mV
1260 design capacity warning: 300 mAh
1261 design capacity low: 200 mAh
1262 capacity granularity 1: 32 mAh
1263 capacity granularity 2: 32 mAh
1265 serial number: 16922
1271 hipo@lepakko conky $ cat /proc/acpi/battery/BAT1/state
1274 charging state: unknown
1276 remaining capacity: 4064 mAh
1277 present voltage: 16608 mV
1281 2213<@jupet�kellari��> jupet@lagi-unstable:~$ cat /proc/apm
1282 2213<@jupet�kellari��> 1.16 1.2 0x03 0x01 0xff 0x10 -1% -1 ?
1283 2213<@jupet�kellari��> (-1 ollee ei akkua kiinni, koska akku on p�yd�ll�)
1284 2214<@jupet�kellari��> jupet@lagi-unstable:~$ cat /proc/apm
1285 2214<@jupet�kellari��> 1.16 1.2 0x03 0x01 0x03 0x09 98% -1 ?
1287 2238<@jupet�kellari��> 1.16 1.2 0x03 0x00 0x00 0x01 100% -1 ? ilman verkkovirtaa
1288 2239<@jupet�kellari��> 1.16 1.2 0x03 0x01 0x00 0x01 99% -1 ? verkkovirralla
1290 2240<@jupet�kellari��> 1.16 1.2 0x03 0x01 0x03 0x09 100% -1 ? verkkovirralla ja monitori p��ll�
1291 2241<@jupet�kellari��> 1.16 1.2 0x03 0x00 0x00 0x01 99% -1 ? monitori p��ll� mutta ilman verkkovirtaa
1294 #define ACPI_BATTERY_BASE_PATH "/proc/acpi/battery"
1295 #define APM_PATH "/proc/apm"
1296 #define MAX_BATTERY_COUNT 4
1298 static FILE *acpi_bat_fp[MAX_BATTERY_COUNT];
1299 static FILE *apm_bat_fp[MAX_BATTERY_COUNT];
1301 static int batteries_initialized = 0;
1302 static char batteries[MAX_BATTERY_COUNT][32];
1304 static int acpi_last_full[MAX_BATTERY_COUNT];
1305 static int acpi_design_capacity[MAX_BATTERY_COUNT];
1307 /* e.g. "charging 75%" */
1308 static char last_battery_str[MAX_BATTERY_COUNT][64];
1310 static char last_battery_time_str[MAX_BATTERY_COUNT][64];
1312 static double last_battery_time[MAX_BATTERY_COUNT];
1314 static int last_battery_perct[MAX_BATTERY_COUNT];
1315 static double last_battery_perct_time[MAX_BATTERY_COUNT];
1317 void init_batteries(void)
1321 if (batteries_initialized) {
1324 for (idx = 0; idx < MAX_BATTERY_COUNT; idx++) {
1325 batteries[idx][0] = '\0';
1327 batteries_initialized = 1;
1330 int get_battery_idx(const char *bat)
1334 for (idx = 0; idx < MAX_BATTERY_COUNT; idx++) {
1335 if (!strlen(batteries[idx]) || !strcmp(batteries[idx], bat)) {
1340 /* if not found, enter a new entry */
1341 if (!strlen(batteries[idx])) {
1342 snprintf(batteries[idx], 31, "%s", bat);
1348 void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
1350 static int idx, rep = 0, rep2 = 0;
1351 char acpi_path[128];
1353 snprintf(acpi_path, 127, ACPI_BATTERY_BASE_PATH "/%s/state", bat);
1357 idx = get_battery_idx(bat);
1359 /* don't update battery too often */
1360 if (current_update_time - last_battery_time[idx] < 29.5) {
1361 goto set_return_value;
1364 last_battery_time[idx] = current_update_time;
1366 memset(last_battery_str[idx], 0, sizeof(last_battery_str[idx]));
1367 memset(last_battery_time_str[idx], 0, sizeof(last_battery_time_str[idx]));
1369 /* first try ACPI */
1371 if (acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL) {
1372 acpi_bat_fp[idx] = open_file(acpi_path, &rep);
1375 if (acpi_bat_fp[idx] != NULL) {
1376 int present_rate = -1;
1377 int remaining_capacity = -1;
1378 char charging_state[64];
1381 /* read last full capacity if it's zero */
1382 if (acpi_last_full[idx] == 0) {
1387 snprintf(path, 127, ACPI_BATTERY_BASE_PATH "/%s/info", bat);
1388 fp = open_file(path, &rep);
1393 if (fgets(b, 256, fp) == NULL) {
1396 if (sscanf(b, "last full capacity: %d",
1397 &acpi_last_full[idx]) != 0) {
1406 fseek(acpi_bat_fp[idx], 0, SEEK_SET);
1408 strcpy(charging_state, "unknown");
1410 while (!feof(acpi_bat_fp[idx])) {
1413 if (fgets(buf, 256, acpi_bat_fp[idx]) == NULL) {
1417 /* let's just hope units are ok */
1418 if (strncmp(buf, "present:", 8) == 0) {
1419 sscanf(buf, "present: %4s", present);
1420 } else if (strncmp(buf, "charging state:", 15) == 0) {
1421 sscanf(buf, "charging state: %63s", charging_state);
1422 } else if (strncmp(buf, "present rate:", 13) == 0) {
1423 sscanf(buf, "present rate: %d", &present_rate);
1424 } else if (strncmp(buf, "remaining capacity:", 19) == 0) {
1425 sscanf(buf, "remaining capacity: %d", &remaining_capacity);
1428 /* Hellf[i]re notes that remaining capacity can exceed acpi_last_full */
1429 if (remaining_capacity > acpi_last_full[idx]) {
1430 /* normalize to 100% */
1431 acpi_last_full[idx] = remaining_capacity;
1435 if (strcmp(present, "no") == 0) {
1436 strncpy(last_battery_str[idx], "not present", 64);
1438 } else if (strcmp(charging_state, "charging") == 0) {
1439 if (acpi_last_full[idx] != 0 && present_rate > 0) {
1440 /* e.g. charging 75% */
1441 snprintf(last_battery_str[idx],
1442 sizeof(last_battery_str[idx]) - 1, "charging %i%%",
1443 (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
1445 format_seconds(last_battery_time_str[idx],
1446 sizeof(last_battery_time_str[idx]) - 1,
1447 (long) (((acpi_last_full[idx] - remaining_capacity) *
1448 3600) / present_rate));
1449 } else if (acpi_last_full[idx] != 0 && present_rate <= 0) {
1450 snprintf(last_battery_str[idx],
1451 sizeof(last_battery_str[idx]) - 1, "charging %d%%",
1452 (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
1454 strncpy(last_battery_str[idx], "charging",
1455 sizeof(last_battery_str[idx]) - 1);
1458 } else if (strncmp(charging_state, "discharging", 64) == 0) {
1459 if (present_rate > 0) {
1460 /* e.g. discharging 35% */
1461 snprintf(last_battery_str[idx],
1462 sizeof(last_battery_str[idx]) - 1, "discharging %i%%",
1463 (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
1465 format_seconds(last_battery_time_str[idx],
1466 sizeof(last_battery_time_str[idx]) - 1,
1467 (long) ((remaining_capacity * 3600) / present_rate));
1468 } else if (present_rate == 0) { /* Thanks to Nexox for this one */
1469 snprintf(last_battery_str[idx],
1470 sizeof(last_battery_str[idx]) - 1, "full");
1472 snprintf(last_battery_str[idx],
1473 sizeof(last_battery_str[idx]) - 1, "discharging %d%%",
1474 (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
1477 } else if (strncmp(charging_state, "charged", 64) == 0) {
1478 /* thanks to Lukas Zapletal <lzap@seznam.cz> */
1479 /* Below happens with the second battery on my X40,
1480 * when the second one is empty and the first one being charged. */
1481 if (remaining_capacity == 0) {
1482 strcpy(last_battery_str[idx], "empty");
1484 strcpy(last_battery_str[idx], "charged");
1486 /* unknown, probably full / AC */
1488 if (acpi_last_full[idx] != 0
1489 && remaining_capacity != acpi_last_full[idx]) {
1490 snprintf(last_battery_str[idx], 64, "unknown %d%%",
1491 (int) ((remaining_capacity * 100) / acpi_last_full[idx]));
1493 strncpy(last_battery_str[idx], "AC", 64);
1498 if (apm_bat_fp[idx] == NULL) {
1499 apm_bat_fp[idx] = open_file(APM_PATH, &rep2);
1502 if (apm_bat_fp[idx] != NULL) {
1503 int ac, status, flag, life;
1505 fscanf(apm_bat_fp[idx], "%*s %*s %*x %x %x %x %d%%",
1506 &ac, &status, &flag, &life);
1509 /* could check now that there is ac */
1510 snprintf(last_battery_str[idx], 64, "AC");
1512 /* could check that status == 3 here? */
1513 } else if (ac && life != 100) {
1514 snprintf(last_battery_str[idx], 64, "charging %d%%", life);
1516 snprintf(last_battery_str[idx], 64, "%d%%", life);
1519 /* it seemed to buffer it so file must be closed (or could use
1520 * syscalls directly but I don't feel like coding it now) */
1521 fclose(apm_bat_fp[idx]);
1522 apm_bat_fp[idx] = NULL;
1528 case BATTERY_STATUS:
1529 snprintf(buf, n, "%s", last_battery_str[idx]);
1532 snprintf(buf, n, "%s", last_battery_time_str[idx]);
1539 int get_battery_perct(const char *bat)
1543 char acpi_path[128];
1545 snprintf(acpi_path, 127, ACPI_BATTERY_BASE_PATH "/%s/state", bat);
1549 idx = get_battery_idx(bat);
1551 /* don't update battery too often */
1552 if (current_update_time - last_battery_perct_time[idx] < 30) {
1553 return last_battery_perct[idx];
1555 last_battery_perct_time[idx] = current_update_time;
1557 /* Only check for ACPI */
1559 if (acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL) {
1560 acpi_bat_fp[idx] = open_file(acpi_path, &rep);
1563 int remaining_capacity = -1;
1565 if (acpi_bat_fp[idx] != NULL) {
1566 /* read last full capacity if it's zero */
1567 if (acpi_design_capacity[idx] == 0) {
1572 snprintf(path, 127, ACPI_BATTERY_BASE_PATH "/%s/info", bat);
1573 fp = open_file(path, &rep);
1578 if (fgets(b, 256, fp) == NULL) {
1581 if (sscanf(b, "last full capacity: %d",
1582 &acpi_design_capacity[idx]) != 0) {
1590 fseek(acpi_bat_fp[idx], 0, SEEK_SET);
1592 while (!feof(acpi_bat_fp[idx])) {
1595 if (fgets(buf, 256, acpi_bat_fp[idx]) == NULL) {
1599 if (buf[0] == 'r') {
1600 sscanf(buf, "remaining capacity: %d", &remaining_capacity);
1604 if (remaining_capacity < 0) {
1607 /* compute the battery percentage */
1608 last_battery_perct[idx] =
1609 (int) (((float) remaining_capacity / acpi_design_capacity[idx]) * 100);
1610 return last_battery_perct[idx];
1613 int get_battery_perct_bar(const char *bar)
1617 get_battery_perct(bar);
1618 idx = get_battery_idx(bar);
1619 return (int) (last_battery_perct[idx] * 2.56 - 1);
1622 /* On Apple powerbook and ibook:
1623 $ cat /proc/pmu/battery_0
1630 $ cat /proc/pmu/info
1631 PMU driver version : 2
1632 PMU firmware version : 0c
1637 /* defines as in <linux/pmu.h> */
1638 #define PMU_BATT_PRESENT 0x00000001
1639 #define PMU_BATT_CHARGING 0x00000002
1641 static FILE *pmu_battery_fp;
1642 static FILE *pmu_info_fp;
1643 static char pb_battery_info[3][32];
1644 static double pb_battery_info_update;
1646 #define PMU_PATH "/proc/pmu"
1647 void get_powerbook_batt_info(char *buf, size_t n, int i)
1650 const char *batt_path = PMU_PATH "/battery_0";
1651 const char *info_path = PMU_PATH "/info";
1652 int flags, charge, max_charge, ac = -1;
1655 /* don't update battery too often */
1656 if (current_update_time - pb_battery_info_update < 29.5) {
1657 snprintf(buf, n, "%s", pb_battery_info[i]);
1660 pb_battery_info_update = current_update_time;
1662 if (pmu_battery_fp == NULL) {
1663 pmu_battery_fp = open_file(batt_path, &rep);
1666 if (pmu_battery_fp != NULL) {
1667 rewind(pmu_battery_fp);
1668 while (!feof(pmu_battery_fp)) {
1671 if (fgets(buf, sizeof(buf), pmu_battery_fp) == NULL) {
1675 if (buf[0] == 'f') {
1676 sscanf(buf, "flags : %8x", &flags);
1677 } else if (buf[0] == 'c' && buf[1] == 'h') {
1678 sscanf(buf, "charge : %d", &charge);
1679 } else if (buf[0] == 'm') {
1680 sscanf(buf, "max_charge : %d", &max_charge);
1681 } else if (buf[0] == 't') {
1682 sscanf(buf, "time rem. : %ld", &time);
1686 if (pmu_info_fp == NULL) {
1687 pmu_info_fp = open_file(info_path, &rep);
1690 if (pmu_info_fp != NULL) {
1691 rewind(pmu_info_fp);
1692 while (!feof(pmu_info_fp)) {
1695 if (fgets(buf, sizeof(buf), pmu_info_fp) == NULL) {
1698 if (buf[0] == 'A') {
1699 sscanf(buf, "AC Power : %d", &ac);
1703 /* update status string */
1704 if ((ac && !(flags & PMU_BATT_PRESENT))) {
1705 strcpy(pb_battery_info[PB_BATT_STATUS], "AC");
1706 } else if (ac && (flags & PMU_BATT_PRESENT)
1707 && !(flags & PMU_BATT_CHARGING)) {
1708 strcpy(pb_battery_info[PB_BATT_STATUS], "charged");
1709 } else if ((flags & PMU_BATT_PRESENT) && (flags & PMU_BATT_CHARGING)) {
1710 strcpy(pb_battery_info[PB_BATT_STATUS], "charging");
1712 strcpy(pb_battery_info[PB_BATT_STATUS], "discharging");
1715 /* update percentage string */
1717 pb_battery_info[PB_BATT_PERCENT][0] = 0;
1719 snprintf(pb_battery_info[PB_BATT_PERCENT],
1720 sizeof(pb_battery_info[PB_BATT_PERCENT]), "%d%%",
1721 (charge * 100) / max_charge);
1724 /* update time string */
1725 if (time == 0) { /* fully charged or battery not present */
1726 pb_battery_info[PB_BATT_TIME][0] = 0;
1727 } else if (time < 60 * 60) { /* don't show secs */
1728 format_seconds_short(pb_battery_info[PB_BATT_TIME],
1729 sizeof(pb_battery_info[PB_BATT_TIME]), time);
1731 format_seconds(pb_battery_info[PB_BATT_TIME],
1732 sizeof(pb_battery_info[PB_BATT_TIME]), time);
1735 snprintf(buf, n, "%s", pb_battery_info[i]);
1740 show_nice_processes = 1;
1741 process_find_top(info.cpu, info.memu);
1742 info.first_process = get_first_process();
1745 /* The following ifdefs were adapted from gkrellm */
1746 #include <linux/major.h>
1748 #if !defined(MD_MAJOR)
1752 #if !defined(LVM_BLK_MAJOR)
1753 #define LVM_BLK_MAJOR 58
1756 #if !defined(NBD_MAJOR)
1757 #define NBD_MAJOR 43
1760 void update_diskio()
1762 static unsigned int last = UINT_MAX;
1763 static unsigned int last_read = UINT_MAX;
1764 static unsigned int last_write = UINT_MAX;
1768 char buf[512], devbuf[64];
1769 int major, minor, i;
1770 unsigned int current = 0;
1771 unsigned int current_read = 0;
1772 unsigned int current_write = 0;
1773 unsigned int reads, writes = 0;
1776 if (!(fp = open_file("/proc/diskstats", &rep))) {
1781 /* read reads and writes from all disks (minor = 0), including cd-roms
1782 * and floppies, and sum them up */
1784 fgets(buf, 512, fp);
1785 col_count = sscanf(buf, "%u %u %s %*u %*u %u %*u %*u %*u %u", &major,
1786 &minor, devbuf, &reads, &writes);
1787 /* ignore subdevices (they have only 3 matching entries in their line)
1788 * and virtual devices (LVM, network block devices, RAM disks, Loopback)
1790 * XXX: ignore devices which are part of a SW RAID (MD_MAJOR) */
1791 if (col_count == 5 && major != LVM_BLK_MAJOR && major != NBD_MAJOR
1792 && major != RAMDISK_MAJOR && major != LOOP_MAJOR) {
1793 current += reads + writes;
1794 current_read += reads;
1795 current_write += writes;
1797 col_count = sscanf(buf, "%u %u %s %*u %u %*u %u",
1798 &major, &minor, devbuf, &reads, &writes);
1799 if (col_count != 5) {
1803 for (i = 0; i < MAX_DISKIO_STATS; i++) {
1804 if (diskio_stats[i].dev &&
1805 strcmp(devbuf, diskio_stats[i].dev) == 0) {
1806 diskio_stats[i].current =
1807 (reads + writes - diskio_stats[i].last) / 2;
1808 diskio_stats[i].current_read =
1809 (reads - diskio_stats[i].last_read) / 2;
1810 diskio_stats[i].current_write =
1811 (writes - diskio_stats[i].last_write) / 2;
1812 if (reads + writes < diskio_stats[i].last) {
1813 diskio_stats[i].current = 0;
1815 if (reads < diskio_stats[i].last_read) {
1816 diskio_stats[i].current_read = 0;
1817 diskio_stats[i].current = diskio_stats[i].current_write;
1819 if (writes < diskio_stats[i].last_write) {
1820 diskio_stats[i].current_write = 0;
1821 diskio_stats[i].current = diskio_stats[i].current_read;
1823 diskio_stats[i].last = reads + writes;
1824 diskio_stats[i].last_read = reads;
1825 diskio_stats[i].last_write = writes;
1830 /* since the values in /proc/diststats are absolute, we have to substract
1831 * our last reading. The numbers stand for "sectors read", and we therefore
1832 * have to divide by two to get KB */
1833 int tot = ((double) (current - last) / 2);
1834 int tot_read = ((double) (current_read - last_read) / 2);
1835 int tot_write = ((double) (current_write - last_write) / 2);
1837 if (last_read > current_read) {
1840 if (last_write > current_write) {
1844 if (last > current) {
1845 /* we hit this either if it's the very first time we run this, or
1846 * when /proc/diskstats overflows; while 0 is not correct, it's at
1847 * least not way off */
1851 last_read = current_read;
1852 last_write = current_write;
1855 diskio_read_value = tot_read;
1856 diskio_write_value = tot_write;
1861 /* Here come the IBM ACPI-specific things. For reference, see
1862 * http://ibm-acpi.sourceforge.net/README
1863 * If IBM ACPI is installed, /proc/acpi/ibm contains the following files:
1879 * The content of these files is described in detail in the aforementioned
1880 * README - some of them also in the following functions accessing them.
1881 * Peter Tarjan (ptarjan@citromail.hu) */
1883 #define IBM_ACPI_DIR "/proc/acpi/ibm"
1885 /* get fan speed on IBM/Lenovo laptops running the ibm acpi.
1886 * /proc/acpi/ibm/fan looks like this (3 lines):
1889 commands: enable, disable
1890 * Peter Tarjan (ptarjan@citromail.hu) */
1892 void get_ibm_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
1894 if (!p_client_buffer || client_buffer_size <= 0) {
1899 unsigned int speed = 0;
1902 snprintf(fan, 127, "%s/fan", IBM_ACPI_DIR);
1904 fp = fopen(fan, "r");
1909 if (fgets(line, 255, fp) == NULL) {
1912 if (sscanf(line, "speed: %d", &speed)) {
1917 CRIT_ERR("can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
1918 "ibm* from your Conky config file.", fan, strerror(errno));
1922 snprintf(p_client_buffer, client_buffer_size, "%d", speed);
1925 /* get the measured temperatures from the temperature sensors
1926 * on IBM/Lenovo laptops running the ibm acpi.
1927 * There are 8 values in /proc/acpi/ibm/thermal, and according to
1928 * http://ibm-acpi.sourceforge.net/README
1929 * these mean the following (at least on an IBM R51...)
1930 * 0: CPU (also on the T series laptops)
1931 * 1: Mini PCI Module (?)
1933 * 3: GPU (also on the T series laptops)
1938 * I'm not too sure about those with the question mark, but the values I'm
1939 * reading from *my* thermal file (on a T42p) look realistic for the
1940 * hdd and the battery.
1941 * #5 and #7 are always -128.
1942 * /proc/acpi/ibm/thermal looks like this (1 line):
1943 temperatures: 41 43 31 46 33 -128 29 -128
1944 * Peter Tarjan (ptarjan@citromail.hu) */
1946 static double last_ibm_acpi_temp_time;
1947 void get_ibm_acpi_temps()
1950 /* don't update too often */
1951 if (current_update_time - last_ibm_acpi_temp_time < 10.00) {
1954 last_ibm_acpi_temp_time = current_update_time;
1956 /* if (!p_client_buffer || client_buffer_size <= 0) {
1964 snprintf(thermal, 127, "%s/thermal", IBM_ACPI_DIR);
1965 fp = fopen(thermal, "r");
1971 if (fgets(line, 255, fp) == NULL) {
1974 if (sscanf(line, "temperatures: %d %d %d %d %d %d %d %d",
1975 &ibm_acpi.temps[0], &ibm_acpi.temps[1], &ibm_acpi.temps[2],
1976 &ibm_acpi.temps[3], &ibm_acpi.temps[4], &ibm_acpi.temps[5],
1977 &ibm_acpi.temps[6], &ibm_acpi.temps[7])) {
1982 CRIT_ERR("can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
1983 "ibm* from your Conky config file.", thermal, strerror(errno));
1989 /* get volume (0-14) on IBM/Lenovo laptops running the ibm acpi.
1990 * "Volume" here is none of the mixer volumes, but a "master of masters"
1991 * volume adjusted by the IBM volume keys.
1992 * /proc/acpi/ibm/fan looks like this (4 lines):
1995 commands: up, down, mute
1996 commands: level <level> (<level> is 0-15)
1997 * Peter Tarjan (ptarjan@citromail.hu) */
1999 void get_ibm_acpi_volume(char *p_client_buffer, size_t client_buffer_size)
2001 if (!p_client_buffer || client_buffer_size <= 0) {
2009 snprintf(volume, 127, "%s/volume", IBM_ACPI_DIR);
2010 unsigned int vol = -1;
2013 fp = fopen(volume, "r");
2017 unsigned int read_vol = -1;
2019 if (fgets(line, 255, fp) == NULL) {
2022 if (sscanf(line, "level: %d", &read_vol)) {
2026 if (sscanf(line, "mute: %s", mute)) {
2031 CRIT_ERR("can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
2032 "ibm* from your Conky config file.", volume, strerror(errno));
2037 if (strcmp(mute, "on") == 0) {
2038 snprintf(p_client_buffer, client_buffer_size, "%s", "mute");
2041 snprintf(p_client_buffer, client_buffer_size, "%d", vol);
2046 /* static FILE *fp = NULL; */
2048 /* get LCD brightness on IBM/Lenovo laptops running the ibm acpi.
2049 * /proc/acpi/ibm/brightness looks like this (3 lines):
2052 commands: level <level> (<level> is 0-7)
2053 * Peter Tarjan (ptarjan@citromail.hu) */
2055 void get_ibm_acpi_brightness(char *p_client_buffer, size_t client_buffer_size)
2057 if (!p_client_buffer || client_buffer_size <= 0) {
2062 unsigned int brightness = 0;
2065 snprintf(filename, 127, "%s/brightness", IBM_ACPI_DIR);
2067 fp = fopen(filename, "r");
2072 if (fgets(line, 255, fp) == NULL) {
2075 if (sscanf(line, "level: %d", &brightness)) {
2080 CRIT_ERR("can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
2081 "ibm* from your Conky config file.", filename, strerror(errno));
2086 snprintf(p_client_buffer, client_buffer_size, "%d", brightness);
2089 void update_entropy(void)
2092 const char *entropy_avail = "/proc/sys/kernel/random/entropy_avail";
2093 const char *entropy_poolsize = "/proc/sys/kernel/random/poolsize";
2096 info.entropy.entropy_avail = 0;
2097 info.entropy.poolsize = 0;
2099 if ((fp1 = open_file(entropy_avail, &rep)) == NULL) {
2103 if ((fp2 = open_file(entropy_poolsize, &rep)) == NULL) {
2108 fscanf(fp1, "%u", &info.entropy.entropy_avail);
2109 fscanf(fp2, "%u", &info.entropy.poolsize);
2114 info.mask |= (1 << INFO_ENTROPY);