2 * rd_stats.c: Read system statistics
3 * (C) 1999-2014 by Sebastien GODARD (sysstat <at> orange.fr)
5 ***************************************************************************
6 * This program is free software; you can redistribute it and/or modify it *
7 * under the terms of the GNU General Public License as published by the *
8 * Free Software Foundation; either version 2 of the License, or (at your *
9 * option) any later version. *
11 * This program is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without the implied warranty of MERCHANTABILITY *
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
16 * You should have received a copy of the GNU General Public License along *
17 * with this program; if not, write to the Free Software Foundation, Inc., *
18 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
19 ***************************************************************************
28 #include <sys/types.h>
40 #define _(string) gettext(string)
42 #define _(string) (string)
46 ***************************************************************************
47 * Read CPU statistics and machine uptime.
50 * @st_cpu Structure where stats will be saved.
51 * @nbr Total number of CPU (including cpu "all").
54 * @st_cpu Structure with statistics.
55 * @uptime Machine uptime multiplied by the number of processors.
56 * @uptime0 Machine uptime. Filled only if previously set to zero.
57 ***************************************************************************
59 void read_stat_cpu(struct stats_cpu *st_cpu, int nbr,
60 unsigned long long *uptime, unsigned long long *uptime0)
63 struct stats_cpu *st_cpu_i;
68 if ((fp = fopen(STAT, "r")) == NULL) {
69 fprintf(stderr, _("Cannot open %s: %s\n"), STAT, strerror(errno));
73 while (fgets(line, sizeof(line), fp) != NULL) {
75 if (!strncmp(line, "cpu ", 4)) {
78 * All the fields don't necessarily exist,
79 * depending on the kernel version used.
81 memset(st_cpu, 0, STATS_CPU_SIZE);
84 * Read the number of jiffies spent in the different modes
85 * (user, nice, etc.) among all proc. CPU usage is not reduced
86 * to one processor to avoid rounding problems.
88 sscanf(line + 5, "%llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
98 &st_cpu->cpu_guest_nice);
101 * Compute the uptime of the system in jiffies (1/100ths of a second
103 * Machine uptime is multiplied by the number of processors here.
105 * NB: Don't add cpu_guest/cpu_guest_nice because cpu_user/cpu_nice
106 * already include them.
108 *uptime = st_cpu->cpu_user + st_cpu->cpu_nice +
109 st_cpu->cpu_sys + st_cpu->cpu_idle +
110 st_cpu->cpu_iowait + st_cpu->cpu_hardirq +
111 st_cpu->cpu_steal + st_cpu->cpu_softirq;
114 else if (!strncmp(line, "cpu", 3)) {
116 /* All the fields don't necessarily exist */
117 memset(&sc, 0, STATS_CPU_SIZE);
119 * Read the number of jiffies spent in the different modes
120 * (user, nice, etc) for current proc.
121 * This is done only on SMP machines.
123 sscanf(line + 3, "%d %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
136 if (proc_nb < (nbr - 1)) {
137 st_cpu_i = st_cpu + proc_nb + 1;
141 * else additional CPUs have been dynamically registered
145 if (!proc_nb && !*uptime0) {
147 * Compute uptime reduced to one proc using proc#0.
148 * Done if /proc/uptime was unavailable.
150 * NB: Don't add cpu_guest/cpu_guest_nice because cpu_user/cpu_nice
151 * already include them.
153 *uptime0 = sc.cpu_user + sc.cpu_nice +
154 sc.cpu_sys + sc.cpu_idle +
155 sc.cpu_iowait + sc.cpu_steal +
156 sc.cpu_hardirq + sc.cpu_softirq;
166 ***************************************************************************
167 * Read interrupts statistics from /proc/stat.
170 * @st_irq Structure where stats will be saved.
171 * @nbr Number of interrupts to read, including the total number
175 * @st_irq Structure with statistics.
176 ***************************************************************************
178 void read_stat_irq(struct stats_irq *st_irq, int nbr)
181 struct stats_irq *st_irq_i;
185 if ((fp = fopen(STAT, "r")) == NULL)
188 while (fgets(line, sizeof(line), fp) != NULL) {
190 if (!strncmp(line, "intr ", 5)) {
191 /* Read total number of interrupts received since system boot */
192 sscanf(line + 5, "%llu", &st_irq->irq_nr);
193 pos = strcspn(line + 5, " ") + 5;
195 for (i = 1; i < nbr; i++) {
196 st_irq_i = st_irq + i;
197 sscanf(line + pos, " %llu", &st_irq_i->irq_nr);
198 pos += strcspn(line + pos + 1, " ") + 1;
207 ***************************************************************************
208 * Read memory statistics from /proc/meminfo.
211 * @st_memory Structure where stats will be saved.
214 * @st_memory Structure with statistics.
215 ***************************************************************************
217 void read_meminfo(struct stats_memory *st_memory)
222 if ((fp = fopen(MEMINFO, "r")) == NULL)
225 while (fgets(line, sizeof(line), fp) != NULL) {
227 if (!strncmp(line, "MemTotal:", 9)) {
228 /* Read the total amount of memory in kB */
229 sscanf(line + 9, "%lu", &st_memory->tlmkb);
231 else if (!strncmp(line, "MemFree:", 8)) {
232 /* Read the amount of free memory in kB */
233 sscanf(line + 8, "%lu", &st_memory->frmkb);
235 else if (!strncmp(line, "Buffers:", 8)) {
236 /* Read the amount of buffered memory in kB */
237 sscanf(line + 8, "%lu", &st_memory->bufkb);
239 else if (!strncmp(line, "Cached:", 7)) {
240 /* Read the amount of cached memory in kB */
241 sscanf(line + 7, "%lu", &st_memory->camkb);
243 else if (!strncmp(line, "SwapCached:", 11)) {
244 /* Read the amount of cached swap in kB */
245 sscanf(line + 11, "%lu", &st_memory->caskb);
247 else if (!strncmp(line, "Active:", 7)) {
248 /* Read the amount of active memory in kB */
249 sscanf(line + 7, "%lu", &st_memory->activekb);
251 else if (!strncmp(line, "Inactive:", 9)) {
252 /* Read the amount of inactive memory in kB */
253 sscanf(line + 9, "%lu", &st_memory->inactkb);
255 else if (!strncmp(line, "SwapTotal:", 10)) {
256 /* Read the total amount of swap memory in kB */
257 sscanf(line + 10, "%lu", &st_memory->tlskb);
259 else if (!strncmp(line, "SwapFree:", 9)) {
260 /* Read the amount of free swap memory in kB */
261 sscanf(line + 9, "%lu", &st_memory->frskb);
263 else if (!strncmp(line, "Dirty:", 6)) {
264 /* Read the amount of dirty memory in kB */
265 sscanf(line + 6, "%lu", &st_memory->dirtykb);
267 else if (!strncmp(line, "Committed_AS:", 13)) {
268 /* Read the amount of commited memory in kB */
269 sscanf(line + 13, "%lu", &st_memory->comkb);
277 ***************************************************************************
278 * Read machine uptime, independently of the number of processors.
281 * @uptime Uptime value in jiffies.
282 ***************************************************************************
284 void read_uptime(unsigned long long *uptime)
288 unsigned long up_sec, up_cent;
290 if ((fp = fopen(UPTIME, "r")) == NULL)
293 if (fgets(line, sizeof(line), fp) == NULL) {
298 sscanf(line, "%lu.%lu", &up_sec, &up_cent);
299 *uptime = (unsigned long long) up_sec * HZ +
300 (unsigned long long) up_cent * HZ / 100;
307 /*---------------- BEGIN: FUNCTIONS USED BY SADC ONLY ---------------------*/
310 ***************************************************************************
311 * Replace octal codes in string with their corresponding characters.
314 * @str String to parse.
317 * @str String with octal codes replaced with characters.
318 ***************************************************************************
320 void oct2chr(char *str)
327 while (i < len - 3) {
328 if ((str[i] == '\\') &&
329 (str[i + 1] >= '0') && (str[i + 1] <= '3') &&
330 (str[i + 2] >= '0') && (str[i + 2] <= '7') &&
331 (str[i + 3] >= '0') && (str[i + 3] <= '7')) {
332 /* Octal code found */
333 str[i] = (str[i + 1] - 48) * 64 +
334 (str[i + 2] - 48) * 8 +
336 for (j = i + 4; j <= len; j++) {
346 ***************************************************************************
347 * Read processes (tasks) creation and context switches statistics
351 * @st_pcsw Structure where stats will be saved.
354 * @st_pcsw Structure with statistics.
355 ***************************************************************************
357 void read_stat_pcsw(struct stats_pcsw *st_pcsw)
362 if ((fp = fopen(STAT, "r")) == NULL)
365 while (fgets(line, sizeof(line), fp) != NULL) {
367 if (!strncmp(line, "ctxt ", 5)) {
368 /* Read number of context switches */
369 sscanf(line + 5, "%llu", &st_pcsw->context_switch);
372 else if (!strncmp(line, "processes ", 10)) {
373 /* Read number of processes created since system boot */
374 sscanf(line + 10, "%lu", &st_pcsw->processes);
382 ***************************************************************************
383 * Read queue and load statistics from /proc/loadavg and /proc/stat.
386 * @st_queue Structure where stats will be saved.
389 * @st_queue Structure with statistics.
390 ***************************************************************************
392 void read_loadavg(struct stats_queue *st_queue)
398 if ((fp = fopen(LOADAVG, "r")) == NULL)
401 /* Read load averages and queue length */
402 fscanf(fp, "%d.%d %d.%d %d.%d %ld/%d %*d\n",
403 &load_tmp[0], &st_queue->load_avg_1,
404 &load_tmp[1], &st_queue->load_avg_5,
405 &load_tmp[2], &st_queue->load_avg_15,
406 &st_queue->nr_running,
407 &st_queue->nr_threads);
411 st_queue->load_avg_1 += load_tmp[0] * 100;
412 st_queue->load_avg_5 += load_tmp[1] * 100;
413 st_queue->load_avg_15 += load_tmp[2] * 100;
415 if (st_queue->nr_running) {
416 /* Do not take current process into account */
417 st_queue->nr_running--;
420 /* Read nr of tasks blocked from /proc/stat */
421 if ((fp = fopen(STAT, "r")) == NULL)
424 while (fgets(line, sizeof(line), fp) != NULL) {
426 if (!strncmp(line, "procs_blocked ", 14)) {
427 /* Read number of processes blocked */
428 sscanf(line + 14, "%lu", &st_queue->procs_blocked);
437 ***************************************************************************
438 * Read swapping statistics from /proc/vmstat.
441 * @st_swap Structure where stats will be saved.
444 * @st_swap Structure with statistics.
445 ***************************************************************************
447 void read_vmstat_swap(struct stats_swap *st_swap)
452 if ((fp = fopen(VMSTAT, "r")) == NULL)
455 while (fgets(line, sizeof(line), fp) != NULL) {
457 if (!strncmp(line, "pswpin ", 7)) {
458 /* Read number of swap pages brought in */
459 sscanf(line + 7, "%lu", &st_swap->pswpin);
461 else if (!strncmp(line, "pswpout ", 8)) {
462 /* Read number of swap pages brought out */
463 sscanf(line + 8, "%lu", &st_swap->pswpout);
471 ***************************************************************************
472 * Read paging statistics from /proc/vmstat.
475 * @st_paging Structure where stats will be saved.
478 * @st_paging Structure with statistics.
479 ***************************************************************************
481 void read_vmstat_paging(struct stats_paging *st_paging)
487 if ((fp = fopen(VMSTAT, "r")) == NULL)
490 st_paging->pgsteal = 0;
491 st_paging->pgscan_kswapd = st_paging->pgscan_direct = 0;
493 while (fgets(line, sizeof(line), fp) != NULL) {
495 if (!strncmp(line, "pgpgin ", 7)) {
496 /* Read number of pages the system paged in */
497 sscanf(line + 7, "%lu", &st_paging->pgpgin);
499 else if (!strncmp(line, "pgpgout ", 8)) {
500 /* Read number of pages the system paged out */
501 sscanf(line + 8, "%lu", &st_paging->pgpgout);
503 else if (!strncmp(line, "pgfault ", 8)) {
504 /* Read number of faults (major+minor) made by the system */
505 sscanf(line + 8, "%lu", &st_paging->pgfault);
507 else if (!strncmp(line, "pgmajfault ", 11)) {
508 /* Read number of faults (major only) made by the system */
509 sscanf(line + 11, "%lu", &st_paging->pgmajfault);
511 else if (!strncmp(line, "pgfree ", 7)) {
512 /* Read number of pages freed by the system */
513 sscanf(line + 7, "%lu", &st_paging->pgfree);
515 else if (!strncmp(line, "pgsteal_", 8)) {
516 /* Read number of pages stolen by the system */
517 sscanf(strchr(line, ' '), "%lu", &pgtmp);
518 st_paging->pgsteal += pgtmp;
520 else if (!strncmp(line, "pgscan_kswapd_", 14)) {
521 /* Read number of pages scanned by the kswapd daemon */
522 sscanf(strchr(line, ' '), "%lu", &pgtmp);
523 st_paging->pgscan_kswapd += pgtmp;
525 else if (!strncmp(line, "pgscan_direct_", 14)) {
526 /* Read number of pages scanned directly */
527 sscanf(strchr(line, ' '), "%lu", &pgtmp);
528 st_paging->pgscan_direct += pgtmp;
536 ***************************************************************************
537 * Read I/O and transfer rates statistics from /proc/diskstats.
540 * @st_io Structure where stats will be saved.
543 * @st_io Structure with statistics.
544 ***************************************************************************
546 void read_diskstats_io(struct stats_io *st_io)
550 char dev_name[MAX_NAME_LEN];
551 unsigned int major, minor;
552 unsigned long rd_ios, wr_ios, rd_sec, wr_sec;
554 if ((fp = fopen(DISKSTATS, "r")) == NULL)
557 while (fgets(line, sizeof(line), fp) != NULL) {
559 if (sscanf(line, "%u %u %s %lu %*u %lu %*u %lu %*u %lu",
560 &major, &minor, dev_name,
561 &rd_ios, &rd_sec, &wr_ios, &wr_sec) == 7) {
563 if (is_device(dev_name, IGNORE_VIRTUAL_DEVICES)) {
565 * OK: It's a (real) device and not a partition.
566 * Note: Structure should have been initialized first!
568 st_io->dk_drive += rd_ios + wr_ios;
569 st_io->dk_drive_rio += rd_ios;
570 st_io->dk_drive_rblk += rd_sec;
571 st_io->dk_drive_wio += wr_ios;
572 st_io->dk_drive_wblk += wr_sec;
581 ***************************************************************************
582 * Read block devices statistics from /proc/diskstats.
585 * @st_disk Structure where stats will be saved.
586 * @nbr Maximum number of block devices.
587 * @read_part True if disks *and* partitions should be read; False if only
591 * @st_disk Structure with statistics.
592 ***************************************************************************
594 void read_diskstats_disk(struct stats_disk *st_disk, int nbr, int read_part)
598 char dev_name[MAX_NAME_LEN];
600 struct stats_disk *st_disk_i;
601 unsigned int major, minor, rd_ticks, wr_ticks, tot_ticks, rq_ticks;
602 unsigned long rd_ios, wr_ios, rd_sec, wr_sec;
604 if ((fp = fopen(DISKSTATS, "r")) == NULL)
607 while ((fgets(line, sizeof(line), fp) != NULL) && (dsk < nbr)) {
609 if (sscanf(line, "%u %u %s %lu %*u %lu %u %lu %*u %lu"
611 &major, &minor, dev_name,
612 &rd_ios, &rd_sec, &rd_ticks, &wr_ios, &wr_sec, &wr_ticks,
613 &tot_ticks, &rq_ticks) == 11) {
615 if (!rd_ios && !wr_ios)
616 /* Unused device: Ignore it */
618 if (read_part || is_device(dev_name, ACCEPT_VIRTUAL_DEVICES)) {
619 st_disk_i = st_disk + dsk++;
620 st_disk_i->major = major;
621 st_disk_i->minor = minor;
622 st_disk_i->nr_ios = rd_ios + wr_ios;
623 st_disk_i->rd_sect = rd_sec;
624 st_disk_i->wr_sect = wr_sec;
625 st_disk_i->rd_ticks = rd_ticks;
626 st_disk_i->wr_ticks = wr_ticks;
627 st_disk_i->tot_ticks = tot_ticks;
628 st_disk_i->rq_ticks = rq_ticks;
637 ***************************************************************************
638 * Read serial lines statistics from /proc/tty/driver/serial.
641 * @st_serial Structure where stats will be saved.
642 * @nbr Maximum number of serial lines.
645 * @st_serial Structure with statistics.
646 ***************************************************************************
648 void read_tty_driver_serial(struct stats_serial *st_serial, int nbr)
651 struct stats_serial *st_serial_i;
656 if ((fp = fopen(SERIAL, "r")) == NULL)
659 while ((fgets(line, sizeof(line), fp) != NULL) && (sl < nbr)) {
661 if ((p = strstr(line, "tx:")) != NULL) {
662 st_serial_i = st_serial + sl;
663 sscanf(line, "%u", &st_serial_i->line);
665 * A value of 0 means an unused structure.
666 * So increment it to make sure it is not null.
668 (st_serial_i->line)++;
670 * Read the number of chars transmitted and received by
671 * current serial line.
673 sscanf(p + 3, "%u", &st_serial_i->tx);
674 if ((p = strstr(line, "rx:")) != NULL) {
675 sscanf(p + 3, "%u", &st_serial_i->rx);
677 if ((p = strstr(line, "fe:")) != NULL) {
678 sscanf(p + 3, "%u", &st_serial_i->frame);
680 if ((p = strstr(line, "pe:")) != NULL) {
681 sscanf(p + 3, "%u", &st_serial_i->parity);
683 if ((p = strstr(line, "brk:")) != NULL) {
684 sscanf(p + 4, "%u", &st_serial_i->brk);
686 if ((p = strstr(line, "oe:")) != NULL) {
687 sscanf(p + 3, "%u", &st_serial_i->overrun);
698 ***************************************************************************
699 * Read kernel tables statistics from various system files.
702 * @st_ktables Structure where stats will be saved.
705 * @st_ktables Structure with statistics.
706 ***************************************************************************
708 void read_kernel_tables(struct stats_ktables *st_ktables)
713 /* Open /proc/sys/fs/dentry-state file */
714 if ((fp = fopen(FDENTRY_STATE, "r")) != NULL) {
716 &st_ktables->dentry_stat);
720 /* Open /proc/sys/fs/file-nr file */
721 if ((fp = fopen(FFILE_NR, "r")) != NULL) {
723 &st_ktables->file_used, &parm);
726 * The number of used handles is the number of allocated ones
727 * minus the number of free ones.
729 st_ktables->file_used -= parm;
732 /* Open /proc/sys/fs/inode-state file */
733 if ((fp = fopen(FINODE_STATE, "r")) != NULL) {
735 &st_ktables->inode_used, &parm);
738 * The number of inuse inodes is the number of allocated ones
739 * minus the number of free ones.
741 st_ktables->inode_used -= parm;
744 /* Open /proc/sys/kernel/pty/nr file */
745 if ((fp = fopen(PTY_NR, "r")) != NULL) {
747 &st_ktables->pty_nr);
753 ***************************************************************************
754 * Read network interfaces statistics from /proc/net/dev.
757 * @st_net_dev Structure where stats will be saved.
758 * @nbr Maximum number of network interfaces.
761 * @st_net_dev Structure with statistics.
764 * Number of interfaces for which stats have been read.
765 ***************************************************************************
767 int read_net_dev(struct stats_net_dev *st_net_dev, int nbr)
770 struct stats_net_dev *st_net_dev_i;
772 char iface[MAX_IFACE_LEN];
776 if ((fp = fopen(NET_DEV, "r")) == NULL)
779 while ((fgets(line, sizeof(line), fp) != NULL) && (dev < nbr)) {
781 pos = strcspn(line, ":");
782 if (pos < strlen(line)) {
783 st_net_dev_i = st_net_dev + dev;
784 strncpy(iface, line, MINIMUM(pos, MAX_IFACE_LEN - 1));
785 iface[MINIMUM(pos, MAX_IFACE_LEN - 1)] = '\0';
786 sscanf(iface, "%s", st_net_dev_i->interface); /* Skip heading spaces */
787 sscanf(line + pos + 1, "%llu %llu %*u %*u %*u %*u %llu %llu %llu %llu "
788 "%*u %*u %*u %*u %*u %llu",
789 &st_net_dev_i->rx_bytes,
790 &st_net_dev_i->rx_packets,
791 &st_net_dev_i->rx_compressed,
792 &st_net_dev_i->multicast,
793 &st_net_dev_i->tx_bytes,
794 &st_net_dev_i->tx_packets,
795 &st_net_dev_i->tx_compressed);
806 ***************************************************************************
807 * Read duplex and speed data for network interface cards.
810 * @st_net_dev Structure where stats will be saved.
811 * @nbr Real number of network interfaces available.
814 * @st_net_dev Structure with statistics.
815 ***************************************************************************
817 void read_if_info(struct stats_net_dev *st_net_dev, int nbr)
820 struct stats_net_dev *st_net_dev_i;
821 char filename[128], duplex[32];
824 for (dev = 0; dev < nbr; dev++) {
826 st_net_dev_i = st_net_dev + dev;
828 /* Read speed info */
829 sprintf(filename, IF_DUPLEX, st_net_dev_i->interface);
831 if ((fp = fopen(filename, "r")) == NULL)
832 /* Cannot read NIC duplex */
835 n = fscanf(fp, "%31s", duplex);
840 /* Cannot read NIC duplex */
843 if (!strcmp(duplex, K_DUPLEX_FULL)) {
844 st_net_dev_i->duplex = C_DUPLEX_FULL;
846 else if (!strcmp(duplex, K_DUPLEX_HALF)) {
847 st_net_dev_i->duplex = C_DUPLEX_HALF;
852 /* Read speed info */
853 sprintf(filename, IF_SPEED, st_net_dev_i->interface);
855 if ((fp = fopen(filename, "r")) == NULL)
856 /* Cannot read NIC speed */
859 fscanf(fp, "%u", &st_net_dev_i->speed);
867 ***************************************************************************
868 * Read network interfaces errors statistics from /proc/net/dev.
871 * @st_net_edev Structure where stats will be saved.
872 * @nbr Maximum number of network interfaces.
875 * @st_net_edev Structure with statistics.
876 ***************************************************************************
878 void read_net_edev(struct stats_net_edev *st_net_edev, int nbr)
881 struct stats_net_edev *st_net_edev_i;
882 static char line[256];
883 char iface[MAX_IFACE_LEN];
887 if ((fp = fopen(NET_DEV, "r")) == NULL)
890 while ((fgets(line, sizeof(line), fp) != NULL) && (dev < nbr)) {
892 pos = strcspn(line, ":");
893 if (pos < strlen(line)) {
894 st_net_edev_i = st_net_edev + dev;
895 strncpy(iface, line, MINIMUM(pos, MAX_IFACE_LEN - 1));
896 iface[MINIMUM(pos, MAX_IFACE_LEN - 1)] = '\0';
897 sscanf(iface, "%s", st_net_edev_i->interface); /* Skip heading spaces */
898 sscanf(line + pos + 1, "%*u %*u %llu %llu %llu %llu %*u %*u %*u %*u "
899 "%llu %llu %llu %llu %llu",
900 &st_net_edev_i->rx_errors,
901 &st_net_edev_i->rx_dropped,
902 &st_net_edev_i->rx_fifo_errors,
903 &st_net_edev_i->rx_frame_errors,
904 &st_net_edev_i->tx_errors,
905 &st_net_edev_i->tx_dropped,
906 &st_net_edev_i->tx_fifo_errors,
907 &st_net_edev_i->collisions,
908 &st_net_edev_i->tx_carrier_errors);
917 ***************************************************************************
918 * Read NFS client statistics from /proc/net/rpc/nfs.
921 * @st_net_nfs Structure where stats will be saved.
924 * @st_net_nfs Structure with statistics.
925 ***************************************************************************
927 void read_net_nfs(struct stats_net_nfs *st_net_nfs)
931 unsigned int getattcnt = 0, accesscnt = 0, readcnt = 0, writecnt = 0;
933 if ((fp = fopen(NET_RPC_NFS, "r")) == NULL)
936 memset(st_net_nfs, 0, STATS_NET_NFS_SIZE);
938 while (fgets(line, sizeof(line), fp) != NULL) {
940 if (!strncmp(line, "rpc ", 4)) {
941 sscanf(line + 4, "%u %u",
942 &st_net_nfs->nfs_rpccnt, &st_net_nfs->nfs_rpcretrans);
944 else if (!strncmp(line, "proc3 ", 6)) {
945 sscanf(line + 6, "%*u %*u %u %*u %*u %u %*u %u %u",
946 &getattcnt, &accesscnt, &readcnt, &writecnt);
948 st_net_nfs->nfs_getattcnt += getattcnt;
949 st_net_nfs->nfs_accesscnt += accesscnt;
950 st_net_nfs->nfs_readcnt += readcnt;
951 st_net_nfs->nfs_writecnt += writecnt;
953 else if (!strncmp(line, "proc4 ", 6)) {
954 sscanf(line + 6, "%*u %*u %u %u "
955 "%*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %u %u",
956 &readcnt, &writecnt, &accesscnt, &getattcnt);
958 st_net_nfs->nfs_getattcnt += getattcnt;
959 st_net_nfs->nfs_accesscnt += accesscnt;
960 st_net_nfs->nfs_readcnt += readcnt;
961 st_net_nfs->nfs_writecnt += writecnt;
969 ***************************************************************************
970 * Read NFS server statistics from /proc/net/rpc/nfsd.
973 * @st_net_nfsd Structure where stats will be saved.
976 * @st_net_nfsd Structure with statistics.
977 ***************************************************************************
979 void read_net_nfsd(struct stats_net_nfsd *st_net_nfsd)
983 unsigned int getattcnt = 0, accesscnt = 0, readcnt = 0, writecnt = 0;
985 if ((fp = fopen(NET_RPC_NFSD, "r")) == NULL)
988 memset(st_net_nfsd, 0, STATS_NET_NFSD_SIZE);
990 while (fgets(line, sizeof(line), fp) != NULL) {
992 if (!strncmp(line, "rc ", 3)) {
993 sscanf(line + 3, "%u %u",
994 &st_net_nfsd->nfsd_rchits, &st_net_nfsd->nfsd_rcmisses);
996 else if (!strncmp(line, "net ", 4)) {
997 sscanf(line + 4, "%u %u %u",
998 &st_net_nfsd->nfsd_netcnt, &st_net_nfsd->nfsd_netudpcnt,
999 &st_net_nfsd->nfsd_nettcpcnt);
1001 else if (!strncmp(line, "rpc ", 4)) {
1002 sscanf(line + 4, "%u %u",
1003 &st_net_nfsd->nfsd_rpccnt, &st_net_nfsd->nfsd_rpcbad);
1005 else if (!strncmp(line, "proc3 ", 6)) {
1006 sscanf(line + 6, "%*u %*u %u %*u %*u %u %*u %u %u",
1007 &getattcnt, &accesscnt, &readcnt, &writecnt);
1009 st_net_nfsd->nfsd_getattcnt += getattcnt;
1010 st_net_nfsd->nfsd_accesscnt += accesscnt;
1011 st_net_nfsd->nfsd_readcnt += readcnt;
1012 st_net_nfsd->nfsd_writecnt += writecnt;
1015 else if (!strncmp(line, "proc4ops ", 9)) {
1016 sscanf(line + 9, "%*u %*u %*u %*u %u "
1017 "%*u %*u %*u %*u %*u %u "
1018 "%*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %u "
1019 "%*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %u",
1020 &accesscnt, &getattcnt, &readcnt, &writecnt);
1022 st_net_nfsd->nfsd_getattcnt += getattcnt;
1023 st_net_nfsd->nfsd_accesscnt += accesscnt;
1024 st_net_nfsd->nfsd_readcnt += readcnt;
1025 st_net_nfsd->nfsd_writecnt += writecnt;
1033 ***************************************************************************
1034 * Read network sockets statistics from /proc/net/sockstat.
1037 * @st_net_sock Structure where stats will be saved.
1040 * @st_net_sock Structure with statistics.
1041 ***************************************************************************
1043 void read_net_sock(struct stats_net_sock *st_net_sock)
1049 if ((fp = fopen(NET_SOCKSTAT, "r")) == NULL)
1052 while (fgets(line, sizeof(line), fp) != NULL) {
1054 if (!strncmp(line, "sockets:", 8)) {
1056 sscanf(line + 14, "%u", &st_net_sock->sock_inuse);
1058 else if (!strncmp(line, "TCP:", 4)) {
1060 sscanf(line + 11, "%u", &st_net_sock->tcp_inuse);
1061 if ((p = strstr(line, "tw")) != NULL) {
1062 sscanf(p + 2, "%u", &st_net_sock->tcp_tw);
1065 else if (!strncmp(line, "UDP:", 4)) {
1067 sscanf(line + 11, "%u", &st_net_sock->udp_inuse);
1069 else if (!strncmp(line, "RAW:", 4)) {
1071 sscanf(line + 11, "%u", &st_net_sock->raw_inuse);
1073 else if (!strncmp(line, "FRAG:", 5)) {
1075 sscanf(line + 12, "%u", &st_net_sock->frag_inuse);
1083 ***************************************************************************
1084 * Read IP network traffic statistics from /proc/net/snmp.
1087 * @st_net_ip Structure where stats will be saved.
1090 * @st_net_ip Structure with statistics.
1091 ***************************************************************************
1093 void read_net_ip(struct stats_net_ip *st_net_ip)
1099 if ((fp = fopen(NET_SNMP, "r")) == NULL)
1102 while (fgets(line, sizeof(line), fp) != NULL) {
1104 if (!strncmp(line, "Ip:", 3)) {
1106 sscanf(line + 3, "%*u %*u %llu %*u %*u %llu %*u %*u "
1107 "%llu %llu %*u %*u %*u %llu %llu %*u %llu %*u %llu",
1108 &st_net_ip->InReceives,
1109 &st_net_ip->ForwDatagrams,
1110 &st_net_ip->InDelivers,
1111 &st_net_ip->OutRequests,
1112 &st_net_ip->ReasmReqds,
1113 &st_net_ip->ReasmOKs,
1114 &st_net_ip->FragOKs,
1115 &st_net_ip->FragCreates);
1129 ***************************************************************************
1130 * Read IP network error statistics from /proc/net/snmp.
1133 * @st_net_eip Structure where stats will be saved.
1136 * @st_net_eip Structure with statistics.
1137 ***************************************************************************
1139 void read_net_eip(struct stats_net_eip *st_net_eip)
1145 if ((fp = fopen(NET_SNMP, "r")) == NULL)
1148 while (fgets(line, sizeof(line), fp) != NULL) {
1150 if (!strncmp(line, "Ip:", 3)) {
1152 sscanf(line + 3, "%*u %*u %*u %llu %llu %*u %llu %llu "
1153 "%*u %*u %llu %llu %*u %*u %*u %llu %*u %llu",
1154 &st_net_eip->InHdrErrors,
1155 &st_net_eip->InAddrErrors,
1156 &st_net_eip->InUnknownProtos,
1157 &st_net_eip->InDiscards,
1158 &st_net_eip->OutDiscards,
1159 &st_net_eip->OutNoRoutes,
1160 &st_net_eip->ReasmFails,
1161 &st_net_eip->FragFails);
1175 ***************************************************************************
1176 * Read ICMP network traffic statistics from /proc/net/snmp.
1179 * @st_net_icmp Structure where stats will be saved.
1182 * @st_net_icmp Structure with statistics.
1183 ***************************************************************************
1185 void read_net_icmp(struct stats_net_icmp *st_net_icmp)
1191 if ((fp = fopen(NET_SNMP, "r")) == NULL)
1194 while (fgets(line, sizeof(line), fp) != NULL) {
1196 if (!strncmp(line, "Icmp:", 5)) {
1198 sscanf(line + 5, "%lu %*u %*u %*u %*u %*u %*u "
1199 "%lu %lu %lu %lu %lu %lu %lu %*u %*u %*u %*u "
1200 "%*u %*u %lu %lu %lu %lu %lu %lu",
1201 &st_net_icmp->InMsgs,
1202 &st_net_icmp->InEchos,
1203 &st_net_icmp->InEchoReps,
1204 &st_net_icmp->InTimestamps,
1205 &st_net_icmp->InTimestampReps,
1206 &st_net_icmp->InAddrMasks,
1207 &st_net_icmp->InAddrMaskReps,
1208 &st_net_icmp->OutMsgs,
1209 &st_net_icmp->OutEchos,
1210 &st_net_icmp->OutEchoReps,
1211 &st_net_icmp->OutTimestamps,
1212 &st_net_icmp->OutTimestampReps,
1213 &st_net_icmp->OutAddrMasks,
1214 &st_net_icmp->OutAddrMaskReps);
1228 ***************************************************************************
1229 * Read ICMP network error statistics from /proc/net/snmp.
1232 * @st_net_eicmp Structure where stats will be saved.
1235 * @st_net_eicmp Structure with statistics.
1236 ***************************************************************************
1238 void read_net_eicmp(struct stats_net_eicmp *st_net_eicmp)
1244 if ((fp = fopen(NET_SNMP, "r")) == NULL)
1247 while (fgets(line, sizeof(line), fp) != NULL) {
1249 if (!strncmp(line, "Icmp:", 5)) {
1251 sscanf(line + 5, "%*u %lu %lu %lu %lu %lu %lu %*u %*u "
1252 "%*u %*u %*u %*u %*u %lu %lu %lu %lu %lu %lu",
1253 &st_net_eicmp->InErrors,
1254 &st_net_eicmp->InDestUnreachs,
1255 &st_net_eicmp->InTimeExcds,
1256 &st_net_eicmp->InParmProbs,
1257 &st_net_eicmp->InSrcQuenchs,
1258 &st_net_eicmp->InRedirects,
1259 &st_net_eicmp->OutErrors,
1260 &st_net_eicmp->OutDestUnreachs,
1261 &st_net_eicmp->OutTimeExcds,
1262 &st_net_eicmp->OutParmProbs,
1263 &st_net_eicmp->OutSrcQuenchs,
1264 &st_net_eicmp->OutRedirects);
1278 ***************************************************************************
1279 * Read TCP network traffic statistics from /proc/net/snmp.
1282 * @st_net_tcp Structure where stats will be saved.
1285 * @st_net_tcp Structure with statistics.
1286 ***************************************************************************
1288 void read_net_tcp(struct stats_net_tcp *st_net_tcp)
1294 if ((fp = fopen(NET_SNMP, "r")) == NULL)
1297 while (fgets(line, sizeof(line), fp) != NULL) {
1299 if (!strncmp(line, "Tcp:", 4)) {
1301 sscanf(line + 4, "%*u %*u %*u %*d %lu %lu "
1302 "%*u %*u %*u %lu %lu",
1303 &st_net_tcp->ActiveOpens,
1304 &st_net_tcp->PassiveOpens,
1305 &st_net_tcp->InSegs,
1306 &st_net_tcp->OutSegs);
1320 ***************************************************************************
1321 * Read TCP network error statistics from /proc/net/snmp.
1324 * @st_net_etcp Structure where stats will be saved.
1327 * @st_net_etcp Structure with statistics.
1328 ***************************************************************************
1330 void read_net_etcp(struct stats_net_etcp *st_net_etcp)
1336 if ((fp = fopen(NET_SNMP, "r")) == NULL)
1339 while (fgets(line, sizeof(line), fp) != NULL) {
1341 if (!strncmp(line, "Tcp:", 4)) {
1343 sscanf(line + 4, "%*u %*u %*u %*d %*u %*u "
1344 "%lu %lu %*u %*u %*u %lu %lu %lu",
1345 &st_net_etcp->AttemptFails,
1346 &st_net_etcp->EstabResets,
1347 &st_net_etcp->RetransSegs,
1348 &st_net_etcp->InErrs,
1349 &st_net_etcp->OutRsts);
1363 ***************************************************************************
1364 * Read UDP network traffic statistics from /proc/net/snmp.
1367 * @st_net_udp Structure where stats will be saved.
1370 * @st_net_udp Structure with statistics.
1371 ***************************************************************************
1373 void read_net_udp(struct stats_net_udp *st_net_udp)
1379 if ((fp = fopen(NET_SNMP, "r")) == NULL)
1382 while (fgets(line, sizeof(line), fp) != NULL) {
1384 if (!strncmp(line, "Udp:", 4)) {
1386 sscanf(line + 4, "%lu %lu %lu %lu",
1387 &st_net_udp->InDatagrams,
1388 &st_net_udp->NoPorts,
1389 &st_net_udp->InErrors,
1390 &st_net_udp->OutDatagrams);
1404 ***************************************************************************
1405 * Read IPv6 network sockets statistics from /proc/net/sockstat6.
1408 * @st_net_sock6 Structure where stats will be saved.
1411 * @st_net_sock6 Structure with statistics.
1412 ***************************************************************************
1414 void read_net_sock6(struct stats_net_sock6 *st_net_sock6)
1419 if ((fp = fopen(NET_SOCKSTAT6, "r")) == NULL)
1422 while (fgets(line, sizeof(line), fp) != NULL) {
1424 if (!strncmp(line, "TCP6:", 5)) {
1426 sscanf(line + 12, "%u", &st_net_sock6->tcp6_inuse);
1428 else if (!strncmp(line, "UDP6:", 5)) {
1430 sscanf(line + 12, "%u", &st_net_sock6->udp6_inuse);
1432 else if (!strncmp(line, "RAW6:", 5)) {
1433 /* IPv6 RAW sockets */
1434 sscanf(line + 12, "%u", &st_net_sock6->raw6_inuse);
1436 else if (!strncmp(line, "FRAG6:", 6)) {
1437 /* IPv6 FRAGments */
1438 sscanf(line + 13, "%u", &st_net_sock6->frag6_inuse);
1446 ***************************************************************************
1447 * Read IPv6 network traffic statistics from /proc/net/snmp6.
1450 * @st_net_ip6 Structure where stats will be saved.
1453 * @st_net_ip6 Structure with statistics.
1454 ***************************************************************************
1456 void read_net_ip6(struct stats_net_ip6 *st_net_ip6)
1461 if ((fp = fopen(NET_SNMP6, "r")) == NULL)
1464 while (fgets(line, sizeof(line), fp) != NULL) {
1466 if (!strncmp(line, "Ip6InReceives ", 14)) {
1467 sscanf(line + 14, "%llu", &st_net_ip6->InReceives6);
1469 else if (!strncmp(line, "Ip6OutForwDatagrams ", 20)) {
1470 sscanf(line + 20, "%llu", &st_net_ip6->OutForwDatagrams6);
1472 else if (!strncmp(line, "Ip6InDelivers ", 14)) {
1473 sscanf(line + 14, "%llu", &st_net_ip6->InDelivers6);
1475 else if (!strncmp(line, "Ip6OutRequests ", 15)) {
1476 sscanf(line + 15, "%llu", &st_net_ip6->OutRequests6);
1478 else if (!strncmp(line, "Ip6ReasmReqds ", 14)) {
1479 sscanf(line + 14, "%llu", &st_net_ip6->ReasmReqds6);
1481 else if (!strncmp(line, "Ip6ReasmOKs ", 12)) {
1482 sscanf(line + 12, "%llu", &st_net_ip6->ReasmOKs6);
1484 else if (!strncmp(line, "Ip6InMcastPkts ", 15)) {
1485 sscanf(line + 15, "%llu", &st_net_ip6->InMcastPkts6);
1487 else if (!strncmp(line, "Ip6OutMcastPkts ", 16)) {
1488 sscanf(line + 16, "%llu", &st_net_ip6->OutMcastPkts6);
1490 else if (!strncmp(line, "Ip6FragOKs ", 11)) {
1491 sscanf(line + 11, "%llu", &st_net_ip6->FragOKs6);
1493 else if (!strncmp(line, "Ip6FragCreates ", 15)) {
1494 sscanf(line + 15, "%llu", &st_net_ip6->FragCreates6);
1502 ***************************************************************************
1503 * Read IPv6 network error statistics from /proc/net/snmp6.
1506 * @st_net_eip6 Structure where stats will be saved.
1509 * @st_net_eip6 Structure with statistics.
1510 ***************************************************************************
1512 void read_net_eip6(struct stats_net_eip6 *st_net_eip6)
1517 if ((fp = fopen(NET_SNMP6, "r")) == NULL)
1520 while (fgets(line, sizeof(line), fp) != NULL) {
1522 if (!strncmp(line, "Ip6InHdrErrors ", 15)) {
1523 sscanf(line + 15, "%llu", &st_net_eip6->InHdrErrors6);
1525 else if (!strncmp(line, "Ip6InAddrErrors ", 16)) {
1526 sscanf(line + 16, "%llu", &st_net_eip6->InAddrErrors6);
1528 else if (!strncmp(line, "Ip6InUnknownProtos ", 19)) {
1529 sscanf(line + 19, "%llu", &st_net_eip6->InUnknownProtos6);
1531 else if (!strncmp(line, "Ip6InTooBigErrors ", 18)) {
1532 sscanf(line + 18, "%llu", &st_net_eip6->InTooBigErrors6);
1534 else if (!strncmp(line, "Ip6InDiscards ", 14)) {
1535 sscanf(line + 14, "%llu", &st_net_eip6->InDiscards6);
1537 else if (!strncmp(line, "Ip6OutDiscards ", 15)) {
1538 sscanf(line + 15, "%llu", &st_net_eip6->OutDiscards6);
1540 else if (!strncmp(line, "Ip6InNoRoutes ", 14)) {
1541 sscanf(line + 14, "%llu", &st_net_eip6->InNoRoutes6);
1543 else if (!strncmp(line, "Ip6OutNoRoutes ", 15)) {
1544 sscanf(line + 15, "%llu", &st_net_eip6->OutNoRoutes6);
1546 else if (!strncmp(line, "Ip6ReasmFails ", 14)) {
1547 sscanf(line + 14, "%llu", &st_net_eip6->ReasmFails6);
1549 else if (!strncmp(line, "Ip6FragFails ", 13)) {
1550 sscanf(line + 13, "%llu", &st_net_eip6->FragFails6);
1552 else if (!strncmp(line, "Ip6InTruncatedPkts ", 19)) {
1553 sscanf(line + 19, "%llu", &st_net_eip6->InTruncatedPkts6);
1561 ***************************************************************************
1562 * Read ICMPv6 network traffic statistics from /proc/net/snmp6.
1565 * @st_net_icmp6 Structure where stats will be saved.
1568 * @st_net_icmp6 Structure with statistics.
1569 ***************************************************************************
1571 void read_net_icmp6(struct stats_net_icmp6 *st_net_icmp6)
1576 if ((fp = fopen(NET_SNMP6, "r")) == NULL)
1579 while (fgets(line, sizeof(line), fp) != NULL) {
1581 if (!strncmp(line, "Icmp6InMsgs ", 12)) {
1582 sscanf(line + 12, "%lu", &st_net_icmp6->InMsgs6);
1584 else if (!strncmp(line, "Icmp6OutMsgs ", 13)) {
1585 sscanf(line + 13, "%lu", &st_net_icmp6->OutMsgs6);
1587 else if (!strncmp(line, "Icmp6InEchos ", 13)) {
1588 sscanf(line + 13, "%lu", &st_net_icmp6->InEchos6);
1590 else if (!strncmp(line, "Icmp6InEchoReplies ", 19)) {
1591 sscanf(line + 19, "%lu", &st_net_icmp6->InEchoReplies6);
1593 else if (!strncmp(line, "Icmp6OutEchoReplies ", 20)) {
1594 sscanf(line + 20, "%lu", &st_net_icmp6->OutEchoReplies6);
1596 else if (!strncmp(line, "Icmp6InGroupMembQueries ", 24)) {
1597 sscanf(line + 24, "%lu", &st_net_icmp6->InGroupMembQueries6);
1599 else if (!strncmp(line, "Icmp6InGroupMembResponses ", 26)) {
1600 sscanf(line + 26, "%lu", &st_net_icmp6->InGroupMembResponses6);
1602 else if (!strncmp(line, "Icmp6OutGroupMembResponses ", 27)) {
1603 sscanf(line + 27, "%lu", &st_net_icmp6->OutGroupMembResponses6);
1605 else if (!strncmp(line, "Icmp6InGroupMembReductions ", 27)) {
1606 sscanf(line + 27, "%lu", &st_net_icmp6->InGroupMembReductions6);
1608 else if (!strncmp(line, "Icmp6OutGroupMembReductions ", 28)) {
1609 sscanf(line + 28, "%lu", &st_net_icmp6->OutGroupMembReductions6);
1611 else if (!strncmp(line, "Icmp6InRouterSolicits ", 22)) {
1612 sscanf(line + 22, "%lu", &st_net_icmp6->InRouterSolicits6);
1614 else if (!strncmp(line, "Icmp6OutRouterSolicits ", 23)) {
1615 sscanf(line + 23, "%lu", &st_net_icmp6->OutRouterSolicits6);
1617 else if (!strncmp(line, "Icmp6InRouterAdvertisements ", 28)) {
1618 sscanf(line + 28, "%lu", &st_net_icmp6->InRouterAdvertisements6);
1620 else if (!strncmp(line, "Icmp6InNeighborSolicits ", 24)) {
1621 sscanf(line + 24, "%lu", &st_net_icmp6->InNeighborSolicits6);
1623 else if (!strncmp(line, "Icmp6OutNeighborSolicits ", 25)) {
1624 sscanf(line + 25, "%lu", &st_net_icmp6->OutNeighborSolicits6);
1626 else if (!strncmp(line, "Icmp6InNeighborAdvertisements ", 30)) {
1627 sscanf(line + 30, "%lu", &st_net_icmp6->InNeighborAdvertisements6);
1629 else if (!strncmp(line, "Icmp6OutNeighborAdvertisements ", 31)) {
1630 sscanf(line + 31, "%lu", &st_net_icmp6->OutNeighborAdvertisements6);
1638 ***************************************************************************
1639 * Read ICMPv6 network error statistics from /proc/net/snmp6.
1642 * @st_net_eicmp6 Structure where stats will be saved.
1645 * @st_net_eicmp6 Structure with statistics.
1646 ***************************************************************************
1648 void read_net_eicmp6(struct stats_net_eicmp6 *st_net_eicmp6)
1653 if ((fp = fopen(NET_SNMP6, "r")) == NULL)
1656 while (fgets(line, sizeof(line), fp) != NULL) {
1658 if (!strncmp(line, "Icmp6InErrors ", 14)) {
1659 sscanf(line + 14, "%lu", &st_net_eicmp6->InErrors6);
1661 else if (!strncmp(line, "Icmp6InDestUnreachs ", 20)) {
1662 sscanf(line + 20, "%lu", &st_net_eicmp6->InDestUnreachs6);
1664 else if (!strncmp(line, "Icmp6OutDestUnreachs ", 21)) {
1665 sscanf(line + 21, "%lu", &st_net_eicmp6->OutDestUnreachs6);
1667 else if (!strncmp(line, "Icmp6InTimeExcds ", 17)) {
1668 sscanf(line + 17, "%lu", &st_net_eicmp6->InTimeExcds6);
1670 else if (!strncmp(line, "Icmp6OutTimeExcds ", 18)) {
1671 sscanf(line + 18, "%lu", &st_net_eicmp6->OutTimeExcds6);
1673 else if (!strncmp(line, "Icmp6InParmProblems ", 20)) {
1674 sscanf(line + 20, "%lu", &st_net_eicmp6->InParmProblems6);
1676 else if (!strncmp(line, "Icmp6OutParmProblems ", 21)) {
1677 sscanf(line + 21, "%lu", &st_net_eicmp6->OutParmProblems6);
1679 else if (!strncmp(line, "Icmp6InRedirects ", 17)) {
1680 sscanf(line + 17, "%lu", &st_net_eicmp6->InRedirects6);
1682 else if (!strncmp(line, "Icmp6OutRedirects ", 18)) {
1683 sscanf(line + 18, "%lu", &st_net_eicmp6->OutRedirects6);
1685 else if (!strncmp(line, "Icmp6InPktTooBigs ", 18)) {
1686 sscanf(line + 18, "%lu", &st_net_eicmp6->InPktTooBigs6);
1688 else if (!strncmp(line, "Icmp6OutPktTooBigs ", 19)) {
1689 sscanf(line + 19, "%lu", &st_net_eicmp6->OutPktTooBigs6);
1697 ***************************************************************************
1698 * Read UDPv6 network traffic statistics from /proc/net/snmp6.
1701 * @st_net_udp6 Structure where stats will be saved.
1704 * @st_net_udp6 Structure with statistics.
1705 ***************************************************************************
1707 void read_net_udp6(struct stats_net_udp6 *st_net_udp6)
1712 if ((fp = fopen(NET_SNMP6, "r")) == NULL)
1715 while (fgets(line, sizeof(line), fp) != NULL) {
1717 if (!strncmp(line, "Udp6InDatagrams ", 16)) {
1718 sscanf(line + 16, "%lu", &st_net_udp6->InDatagrams6);
1720 else if (!strncmp(line, "Udp6OutDatagrams ", 17)) {
1721 sscanf(line + 17, "%lu", &st_net_udp6->OutDatagrams6);
1723 else if (!strncmp(line, "Udp6NoPorts ", 12)) {
1724 sscanf(line + 12, "%lu", &st_net_udp6->NoPorts6);
1726 else if (!strncmp(line, "Udp6InErrors ", 13)) {
1727 sscanf(line + 13, "%lu", &st_net_udp6->InErrors6);
1735 ***************************************************************************
1736 * Read CPU frequency statistics.
1739 * @st_pwr_cpufreq Structure where stats will be saved.
1740 * @nbr Total number of CPU (including cpu "all").
1743 * @st_pwr_cpufreq Structure with statistics.
1744 ***************************************************************************
1746 void read_cpuinfo(struct stats_pwr_cpufreq *st_pwr_cpufreq, int nbr)
1749 struct stats_pwr_cpufreq *st_pwr_cpufreq_i;
1752 unsigned int proc_nb = 0, ifreq, dfreq;
1754 if ((fp = fopen(CPUINFO, "r")) == NULL)
1757 st_pwr_cpufreq->cpufreq = 0;
1759 while (fgets(line, sizeof(line), fp) != NULL) {
1761 if (!strncmp(line, "processor\t", 10)) {
1762 sscanf(strchr(line, ':') + 1, "%u", &proc_nb);
1765 else if (!strncmp(line, "cpu MHz\t", 8)) {
1766 sscanf(strchr(line, ':') + 1, "%u.%u", &ifreq, &dfreq);
1768 if (proc_nb < (nbr - 1)) {
1769 /* Save current CPU frequency */
1770 st_pwr_cpufreq_i = st_pwr_cpufreq + proc_nb + 1;
1771 st_pwr_cpufreq_i->cpufreq = ifreq * 100 + dfreq / 10;
1773 /* Also save it to compute an average CPU frequency */
1774 st_pwr_cpufreq->cpufreq += st_pwr_cpufreq_i->cpufreq;
1777 else if (!proc_nb && (nbr == 1)) {
1779 * We are reading freq for "Processor 0" and we have a machine
1780 * with only one processor and not an SMP kernel, with /sys not mounted
1781 * (the nr of proc has been counted using /proc/stat and there was
1782 * only one line with global CPU stats here).
1783 * This is a very specific case, I must admit...
1785 st_pwr_cpufreq->cpufreq = ifreq * 100 + dfreq / 10;
1793 /* Compute average CPU frequency for this machine */
1794 st_pwr_cpufreq->cpufreq /= nr;
1799 ***************************************************************************
1800 * Read hugepages statistics from /proc/meminfo.
1803 * @st_huge Structure where stats will be saved.
1806 * @st_huge Structure with statistics.
1807 ***************************************************************************
1809 void read_meminfo_huge(struct stats_huge *st_huge)
1813 unsigned long szhkb = 0;
1815 if ((fp = fopen(MEMINFO, "r")) == NULL)
1818 while (fgets(line, sizeof(line), fp) != NULL) {
1820 if (!strncmp(line, "HugePages_Total:", 16)) {
1821 /* Read the total number of huge pages */
1822 sscanf(line + 16, "%lu", &st_huge->tlhkb);
1824 else if (!strncmp(line, "HugePages_Free:", 15)) {
1825 /* Read the number of free huge pages */
1826 sscanf(line + 15, "%lu", &st_huge->frhkb);
1828 else if (!strncmp(line, "Hugepagesize:", 13)) {
1829 /* Read the default size of a huge page in kB */
1830 sscanf(line + 13, "%lu", &szhkb);
1836 /* We want huge pages stats in kB and not expressed in a number of pages */
1837 st_huge->tlhkb *= szhkb;
1838 st_huge->frhkb *= szhkb;
1842 ***************************************************************************
1843 * Read CPU average frequencies statistics.
1846 * @st_pwr_wghfreq Structure where stats will be saved.
1847 * @cpu_nr CPU number for which time_in_state date will be read.
1848 * @nbr Total number of states (frequencies).
1851 * @st_pwr_wghfreq Structure with statistics.
1852 ***************************************************************************
1854 void read_time_in_state(struct stats_pwr_wghfreq *st_pwr_wghfreq, int cpu_nr, int nbr)
1857 struct stats_pwr_wghfreq *st_pwr_wghfreq_j;
1858 char filename[MAX_PF_NAME];
1862 unsigned long long time_in_state;
1864 snprintf(filename, MAX_PF_NAME, "%s/cpu%d/%s",
1865 SYSFS_DEVCPU, cpu_nr, SYSFS_TIME_IN_STATE);
1866 if ((fp = fopen(filename, "r")) == NULL)
1869 while (fgets(line, sizeof(line), fp) != NULL) {
1871 sscanf(line, "%lu %llu", &freq, &time_in_state);
1874 /* Save current frequency and time */
1875 st_pwr_wghfreq_j = st_pwr_wghfreq + j;
1876 st_pwr_wghfreq_j->freq = freq;
1877 st_pwr_wghfreq_j->time_in_state = time_in_state;
1886 ***************************************************************************
1887 * Read current USB device data.
1890 * @st_pwr_usb Structure where stats will be saved.
1891 * @usb_device File name for current USB device.
1894 * @st_pwr_usb Structure with statistics.
1895 ***************************************************************************
1897 void read_usb_stats(struct stats_pwr_usb *st_pwr_usb, char *usb_device)
1901 char filename[MAX_PF_NAME];
1903 /* Get USB device bus number */
1904 sscanf(usb_device, "%u", &st_pwr_usb->bus_nr);
1906 /* Read USB device vendor ID */
1907 snprintf(filename, MAX_PF_NAME, "%s/%s/%s",
1908 SYSFS_USBDEV, usb_device, SYSFS_IDVENDOR);
1909 if ((fp = fopen(filename, "r")) != NULL) {
1911 &st_pwr_usb->vendor_id);
1915 /* Read USB device product ID */
1916 snprintf(filename, MAX_PF_NAME, "%s/%s/%s",
1917 SYSFS_USBDEV, usb_device, SYSFS_IDPRODUCT);
1918 if ((fp = fopen(filename, "r")) != NULL) {
1920 &st_pwr_usb->product_id);
1924 /* Read USB device max power consumption */
1925 snprintf(filename, MAX_PF_NAME, "%s/%s/%s",
1926 SYSFS_USBDEV, usb_device, SYSFS_BMAXPOWER);
1927 if ((fp = fopen(filename, "r")) != NULL) {
1929 &st_pwr_usb->bmaxpower);
1933 /* Read USB device manufacturer */
1934 snprintf(filename, MAX_PF_NAME, "%s/%s/%s",
1935 SYSFS_USBDEV, usb_device, SYSFS_MANUFACTURER);
1936 if ((fp = fopen(filename, "r")) != NULL) {
1937 fgets(st_pwr_usb->manufacturer, MAX_MANUF_LEN - 1, fp);
1939 if ((l = strlen(st_pwr_usb->manufacturer)) > 0) {
1940 /* Remove trailing CR */
1941 st_pwr_usb->manufacturer[l - 1] = '\0';
1945 /* Read USB device product */
1946 snprintf(filename, MAX_PF_NAME, "%s/%s/%s",
1947 SYSFS_USBDEV, usb_device, SYSFS_PRODUCT);
1948 if ((fp = fopen(filename, "r")) != NULL) {
1949 fgets(st_pwr_usb->product, MAX_PROD_LEN - 1, fp);
1951 if ((l = strlen(st_pwr_usb->product)) > 0) {
1952 /* Remove trailing CR */
1953 st_pwr_usb->product[l - 1] = '\0';
1959 ***************************************************************************
1960 * Read USB devices statistics.
1963 * @st_pwr_usb Structure where stats will be saved.
1964 * @nbr Total number of USB devices.
1967 * @st_pwr_usb Structure with statistics.
1968 ***************************************************************************
1970 void read_bus_usb_dev(struct stats_pwr_usb *st_pwr_usb, int nbr)
1974 struct stats_pwr_usb *st_pwr_usb_j;
1977 /* Open relevant /sys directory */
1978 if ((dir = opendir(SYSFS_USBDEV)) == NULL)
1981 /* Get current file entry */
1982 while ((drd = readdir(dir)) != NULL) {
1984 if (isdigit(drd->d_name[0]) && !strchr(drd->d_name, ':')) {
1986 /* Read current USB device data */
1987 st_pwr_usb_j = st_pwr_usb + j;
1988 read_usb_stats(st_pwr_usb_j, drd->d_name);
1996 /* Close directory */
2001 ***************************************************************************
2002 * Read filesystems statistics.
2005 * @st_filesystem Structure where stats will be saved.
2006 * @nbr Total number of filesystems.
2009 * @st_filesystem Structure with statistics.
2010 ***************************************************************************
2012 void read_filesystem(struct stats_filesystem *st_filesystem, int nbr)
2015 char line[256], fs_name[MAX_FS_LEN], mountp[128];
2017 struct stats_filesystem *st_filesystem_i;
2020 if ((fp = fopen(MTAB, "r")) == NULL)
2023 while ((fgets(line, sizeof(line), fp) != NULL) && (fs < nbr)) {
2024 if (line[0] == '/') {
2026 /* Read current filesystem name and mount point */
2027 sscanf(line, "%71s %127s", fs_name, mountp);
2029 /* Replace octal codes */
2032 if ((statfs(mountp, &buf) < 0) || (!buf.f_blocks))
2035 st_filesystem_i = st_filesystem + fs++;
2036 st_filesystem_i->f_blocks = buf.f_blocks * buf.f_bsize;
2037 st_filesystem_i->f_bfree = buf.f_bfree * buf.f_bsize;
2038 st_filesystem_i->f_bavail = buf.f_bavail * buf.f_bsize;
2039 st_filesystem_i->f_files = buf.f_files;
2040 st_filesystem_i->f_ffree = buf.f_ffree;
2041 strcpy(st_filesystem_i->fs_name, fs_name);
2048 /*------------------ END: FUNCTIONS USED BY SADC ONLY ---------------------*/
2049 #endif /* SOURCE_SADC */