2 * rd_stats.c: Read system statistics
3 * (C) 1999-2016 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA *
19 ***************************************************************************
28 #include <sys/types.h>
30 #include <sys/statvfs.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, "MemAvailable:", 13)) {
236 /* Read the amount of available memory in kB */
237 sscanf(line + 13, "%lu", &st_memory->availablekb);
239 else if (!strncmp(line, "Buffers:", 8)) {
240 /* Read the amount of buffered memory in kB */
241 sscanf(line + 8, "%lu", &st_memory->bufkb);
243 else if (!strncmp(line, "Cached:", 7)) {
244 /* Read the amount of cached memory in kB */
245 sscanf(line + 7, "%lu", &st_memory->camkb);
247 else if (!strncmp(line, "SwapCached:", 11)) {
248 /* Read the amount of cached swap in kB */
249 sscanf(line + 11, "%lu", &st_memory->caskb);
251 else if (!strncmp(line, "Active:", 7)) {
252 /* Read the amount of active memory in kB */
253 sscanf(line + 7, "%lu", &st_memory->activekb);
255 else if (!strncmp(line, "Inactive:", 9)) {
256 /* Read the amount of inactive memory in kB */
257 sscanf(line + 9, "%lu", &st_memory->inactkb);
259 else if (!strncmp(line, "SwapTotal:", 10)) {
260 /* Read the total amount of swap memory in kB */
261 sscanf(line + 10, "%lu", &st_memory->tlskb);
263 else if (!strncmp(line, "SwapFree:", 9)) {
264 /* Read the amount of free swap memory in kB */
265 sscanf(line + 9, "%lu", &st_memory->frskb);
267 else if (!strncmp(line, "Dirty:", 6)) {
268 /* Read the amount of dirty memory in kB */
269 sscanf(line + 6, "%lu", &st_memory->dirtykb);
271 else if (!strncmp(line, "Committed_AS:", 13)) {
272 /* Read the amount of commited memory in kB */
273 sscanf(line + 13, "%lu", &st_memory->comkb);
275 else if (!strncmp(line, "AnonPages:", 10)) {
276 /* Read the amount of pages mapped into userspace page tables in kB */
277 sscanf(line + 10, "%lu", &st_memory->anonpgkb);
279 else if (!strncmp(line, "Slab:", 5)) {
280 /* Read the amount of in-kernel data structures cache in kB */
281 sscanf(line + 5, "%lu", &st_memory->slabkb);
283 else if (!strncmp(line, "KernelStack:", 12)) {
284 /* Read the kernel stack utilization in kB */
285 sscanf(line + 12, "%lu", &st_memory->kstackkb);
287 else if (!strncmp(line, "PageTables:", 11)) {
288 /* Read the amount of memory dedicated to the lowest level of page tables in kB */
289 sscanf(line + 11, "%lu", &st_memory->pgtblkb);
291 else if (!strncmp(line, "VmallocUsed:", 12)) {
292 /* Read the amount of vmalloc area which is used in kB */
293 sscanf(line + 12, "%lu", &st_memory->vmusedkb);
301 ***************************************************************************
302 * Read machine uptime, independently of the number of processors.
305 * @uptime Uptime value in jiffies.
306 ***************************************************************************
308 void read_uptime(unsigned long long *uptime)
312 unsigned long up_sec, up_cent;
314 if ((fp = fopen(UPTIME, "r")) == NULL)
317 if (fgets(line, sizeof(line), fp) == NULL) {
322 sscanf(line, "%lu.%lu", &up_sec, &up_cent);
323 *uptime = (unsigned long long) up_sec * HZ +
324 (unsigned long long) up_cent * HZ / 100;
331 /*---------------- BEGIN: FUNCTIONS USED BY SADC ONLY ---------------------*/
334 ***************************************************************************
335 * Replace octal codes in string with their corresponding characters.
338 * @str String to parse.
341 * @str String with octal codes replaced with characters.
342 ***************************************************************************
344 void oct2chr(char *str)
351 while (i < len - 3) {
352 if ((str[i] == '\\') &&
353 (str[i + 1] >= '0') && (str[i + 1] <= '3') &&
354 (str[i + 2] >= '0') && (str[i + 2] <= '7') &&
355 (str[i + 3] >= '0') && (str[i + 3] <= '7')) {
356 /* Octal code found */
357 str[i] = (str[i + 1] - 48) * 64 +
358 (str[i + 2] - 48) * 8 +
360 for (j = i + 4; j <= len; j++) {
370 ***************************************************************************
371 * Read processes (tasks) creation and context switches statistics
375 * @st_pcsw Structure where stats will be saved.
378 * @st_pcsw Structure with statistics.
379 ***************************************************************************
381 void read_stat_pcsw(struct stats_pcsw *st_pcsw)
386 if ((fp = fopen(STAT, "r")) == NULL)
389 while (fgets(line, sizeof(line), fp) != NULL) {
391 if (!strncmp(line, "ctxt ", 5)) {
392 /* Read number of context switches */
393 sscanf(line + 5, "%llu", &st_pcsw->context_switch);
396 else if (!strncmp(line, "processes ", 10)) {
397 /* Read number of processes created since system boot */
398 sscanf(line + 10, "%lu", &st_pcsw->processes);
406 ***************************************************************************
407 * Read queue and load statistics from /proc/loadavg and /proc/stat.
410 * @st_queue Structure where stats will be saved.
413 * @st_queue Structure with statistics.
414 ***************************************************************************
416 void read_loadavg(struct stats_queue *st_queue)
423 if ((fp = fopen(LOADAVG, "r")) == NULL)
426 /* Read load averages and queue length */
427 rc = fscanf(fp, "%d.%u %d.%u %d.%u %lu/%u %*d\n",
428 &load_tmp[0], &st_queue->load_avg_1,
429 &load_tmp[1], &st_queue->load_avg_5,
430 &load_tmp[2], &st_queue->load_avg_15,
431 &st_queue->nr_running,
432 &st_queue->nr_threads);
439 st_queue->load_avg_1 += load_tmp[0] * 100;
440 st_queue->load_avg_5 += load_tmp[1] * 100;
441 st_queue->load_avg_15 += load_tmp[2] * 100;
443 if (st_queue->nr_running) {
444 /* Do not take current process into account */
445 st_queue->nr_running--;
448 /* Read nr of tasks blocked from /proc/stat */
449 if ((fp = fopen(STAT, "r")) == NULL)
452 while (fgets(line, sizeof(line), fp) != NULL) {
454 if (!strncmp(line, "procs_blocked ", 14)) {
455 /* Read number of processes blocked */
456 sscanf(line + 14, "%lu", &st_queue->procs_blocked);
465 ***************************************************************************
466 * Read swapping statistics from /proc/vmstat.
469 * @st_swap Structure where stats will be saved.
472 * @st_swap Structure with statistics.
473 ***************************************************************************
475 void read_vmstat_swap(struct stats_swap *st_swap)
480 if ((fp = fopen(VMSTAT, "r")) == NULL)
483 while (fgets(line, sizeof(line), fp) != NULL) {
485 if (!strncmp(line, "pswpin ", 7)) {
486 /* Read number of swap pages brought in */
487 sscanf(line + 7, "%lu", &st_swap->pswpin);
489 else if (!strncmp(line, "pswpout ", 8)) {
490 /* Read number of swap pages brought out */
491 sscanf(line + 8, "%lu", &st_swap->pswpout);
499 ***************************************************************************
500 * Read paging statistics from /proc/vmstat.
503 * @st_paging Structure where stats will be saved.
506 * @st_paging Structure with statistics.
507 ***************************************************************************
509 void read_vmstat_paging(struct stats_paging *st_paging)
515 if ((fp = fopen(VMSTAT, "r")) == NULL)
518 st_paging->pgsteal = 0;
519 st_paging->pgscan_kswapd = st_paging->pgscan_direct = 0;
521 while (fgets(line, sizeof(line), fp) != NULL) {
523 if (!strncmp(line, "pgpgin ", 7)) {
524 /* Read number of pages the system paged in */
525 sscanf(line + 7, "%lu", &st_paging->pgpgin);
527 else if (!strncmp(line, "pgpgout ", 8)) {
528 /* Read number of pages the system paged out */
529 sscanf(line + 8, "%lu", &st_paging->pgpgout);
531 else if (!strncmp(line, "pgfault ", 8)) {
532 /* Read number of faults (major+minor) made by the system */
533 sscanf(line + 8, "%lu", &st_paging->pgfault);
535 else if (!strncmp(line, "pgmajfault ", 11)) {
536 /* Read number of faults (major only) made by the system */
537 sscanf(line + 11, "%lu", &st_paging->pgmajfault);
539 else if (!strncmp(line, "pgfree ", 7)) {
540 /* Read number of pages freed by the system */
541 sscanf(line + 7, "%lu", &st_paging->pgfree);
543 else if (!strncmp(line, "pgsteal_", 8)) {
544 /* Read number of pages stolen by the system */
545 sscanf(strchr(line, ' '), "%lu", &pgtmp);
546 st_paging->pgsteal += pgtmp;
548 else if (!strncmp(line, "pgscan_kswapd", 13)) {
549 /* Read number of pages scanned by the kswapd daemon */
550 sscanf(strchr(line, ' '), "%lu", &pgtmp);
551 st_paging->pgscan_kswapd += pgtmp;
553 else if (!strncmp(line, "pgscan_direct", 13)) {
554 /* Read number of pages scanned directly */
555 sscanf(strchr(line, ' '), "%lu", &pgtmp);
556 st_paging->pgscan_direct += pgtmp;
564 ***************************************************************************
565 * Read I/O and transfer rates statistics from /proc/diskstats.
568 * @st_io Structure where stats will be saved.
571 * @st_io Structure with statistics.
572 ***************************************************************************
574 void read_diskstats_io(struct stats_io *st_io)
578 char dev_name[MAX_NAME_LEN];
579 unsigned int major, minor;
580 unsigned long rd_ios, wr_ios, rd_sec, wr_sec;
582 if ((fp = fopen(DISKSTATS, "r")) == NULL)
585 while (fgets(line, sizeof(line), fp) != NULL) {
587 if (sscanf(line, "%u %u %s %lu %*u %lu %*u %lu %*u %lu",
588 &major, &minor, dev_name,
589 &rd_ios, &rd_sec, &wr_ios, &wr_sec) == 7) {
591 if (is_device(dev_name, IGNORE_VIRTUAL_DEVICES)) {
593 * OK: It's a (real) device and not a partition.
594 * Note: Structure should have been initialized first!
596 st_io->dk_drive += rd_ios + wr_ios;
597 st_io->dk_drive_rio += rd_ios;
598 st_io->dk_drive_rblk += rd_sec;
599 st_io->dk_drive_wio += wr_ios;
600 st_io->dk_drive_wblk += wr_sec;
609 ***************************************************************************
610 * Read block devices statistics from /proc/diskstats.
613 * @st_disk Structure where stats will be saved.
614 * @nbr Maximum number of block devices.
615 * @read_part True if disks *and* partitions should be read; False if only
619 * @st_disk Structure with statistics.
620 ***************************************************************************
622 void read_diskstats_disk(struct stats_disk *st_disk, int nbr, int read_part)
626 char dev_name[MAX_NAME_LEN];
628 struct stats_disk *st_disk_i;
629 unsigned int major, minor, rd_ticks, wr_ticks, tot_ticks, rq_ticks;
630 unsigned long rd_ios, wr_ios, rd_sec, wr_sec;
632 if ((fp = fopen(DISKSTATS, "r")) == NULL)
635 while ((fgets(line, sizeof(line), fp) != NULL) && (dsk < nbr)) {
637 if (sscanf(line, "%u %u %s %lu %*u %lu %u %lu %*u %lu"
639 &major, &minor, dev_name,
640 &rd_ios, &rd_sec, &rd_ticks, &wr_ios, &wr_sec, &wr_ticks,
641 &tot_ticks, &rq_ticks) == 11) {
643 if (!rd_ios && !wr_ios)
644 /* Unused device: Ignore it */
646 if (read_part || is_device(dev_name, ACCEPT_VIRTUAL_DEVICES)) {
647 st_disk_i = st_disk + dsk++;
648 st_disk_i->major = major;
649 st_disk_i->minor = minor;
650 st_disk_i->nr_ios = rd_ios + wr_ios;
651 st_disk_i->rd_sect = rd_sec;
652 st_disk_i->wr_sect = wr_sec;
653 st_disk_i->rd_ticks = rd_ticks;
654 st_disk_i->wr_ticks = wr_ticks;
655 st_disk_i->tot_ticks = tot_ticks;
656 st_disk_i->rq_ticks = rq_ticks;
665 ***************************************************************************
666 * Read serial lines statistics from /proc/tty/driver/serial.
669 * @st_serial Structure where stats will be saved.
670 * @nbr Maximum number of serial lines.
673 * @st_serial Structure with statistics.
674 ***************************************************************************
676 void read_tty_driver_serial(struct stats_serial *st_serial, int nbr)
679 struct stats_serial *st_serial_i;
684 if ((fp = fopen(SERIAL, "r")) == NULL)
687 while ((fgets(line, sizeof(line), fp) != NULL) && (sl < nbr)) {
689 if ((p = strstr(line, "tx:")) != NULL) {
690 st_serial_i = st_serial + sl;
691 sscanf(line, "%u", &st_serial_i->line);
693 * A value of 0 means an unused structure.
694 * So increment it to make sure it is not zero.
696 (st_serial_i->line)++;
698 * Read the number of chars transmitted and received by
699 * current serial line.
701 sscanf(p + 3, "%u", &st_serial_i->tx);
702 if ((p = strstr(line, "rx:")) != NULL) {
703 sscanf(p + 3, "%u", &st_serial_i->rx);
705 if ((p = strstr(line, "fe:")) != NULL) {
706 sscanf(p + 3, "%u", &st_serial_i->frame);
708 if ((p = strstr(line, "pe:")) != NULL) {
709 sscanf(p + 3, "%u", &st_serial_i->parity);
711 if ((p = strstr(line, "brk:")) != NULL) {
712 sscanf(p + 4, "%u", &st_serial_i->brk);
714 if ((p = strstr(line, "oe:")) != NULL) {
715 sscanf(p + 3, "%u", &st_serial_i->overrun);
726 ***************************************************************************
727 * Read kernel tables statistics from various system files.
730 * @st_ktables Structure where stats will be saved.
733 * @st_ktables Structure with statistics.
734 ***************************************************************************
736 void read_kernel_tables(struct stats_ktables *st_ktables)
742 /* Open /proc/sys/fs/dentry-state file */
743 if ((fp = fopen(FDENTRY_STATE, "r")) != NULL) {
744 rc = fscanf(fp, "%*d %u",
745 &st_ktables->dentry_stat);
748 st_ktables->dentry_stat = 0;
752 /* Open /proc/sys/fs/file-nr file */
753 if ((fp = fopen(FFILE_NR, "r")) != NULL) {
754 rc = fscanf(fp, "%u %u",
755 &st_ktables->file_used, &parm);
758 * The number of used handles is the number of allocated ones
759 * minus the number of free ones.
762 st_ktables->file_used -= parm;
765 st_ktables->file_used = 0;
769 /* Open /proc/sys/fs/inode-state file */
770 if ((fp = fopen(FINODE_STATE, "r")) != NULL) {
771 rc = fscanf(fp, "%u %u",
772 &st_ktables->inode_used, &parm);
775 * The number of inuse inodes is the number of allocated ones
776 * minus the number of free ones.
779 st_ktables->inode_used -= parm;
782 st_ktables->inode_used = 0;
786 /* Open /proc/sys/kernel/pty/nr file */
787 if ((fp = fopen(PTY_NR, "r")) != NULL) {
788 rc = fscanf(fp, "%u",
789 &st_ktables->pty_nr);
792 st_ktables->pty_nr = 0;
798 ***************************************************************************
799 * Read network interfaces statistics from /proc/net/dev.
802 * @st_net_dev Structure where stats will be saved.
803 * @nbr Maximum number of network interfaces.
806 * @st_net_dev Structure with statistics.
809 * Number of interfaces for which stats have been read.
810 ***************************************************************************
812 int read_net_dev(struct stats_net_dev *st_net_dev, int nbr)
815 struct stats_net_dev *st_net_dev_i;
817 char iface[MAX_IFACE_LEN];
821 if ((fp = fopen(NET_DEV, "r")) == NULL)
824 while ((fgets(line, sizeof(line), fp) != NULL) && (dev < nbr)) {
826 pos = strcspn(line, ":");
827 if (pos < strlen(line)) {
828 st_net_dev_i = st_net_dev + dev;
829 strncpy(iface, line, MINIMUM(pos, MAX_IFACE_LEN - 1));
830 iface[MINIMUM(pos, MAX_IFACE_LEN - 1)] = '\0';
831 sscanf(iface, "%s", st_net_dev_i->interface); /* Skip heading spaces */
832 sscanf(line + pos + 1, "%llu %llu %*u %*u %*u %*u %llu %llu %llu %llu "
833 "%*u %*u %*u %*u %*u %llu",
834 &st_net_dev_i->rx_bytes,
835 &st_net_dev_i->rx_packets,
836 &st_net_dev_i->rx_compressed,
837 &st_net_dev_i->multicast,
838 &st_net_dev_i->tx_bytes,
839 &st_net_dev_i->tx_packets,
840 &st_net_dev_i->tx_compressed);
851 ***************************************************************************
852 * Read duplex and speed data for network interface cards.
855 * @st_net_dev Structure where stats will be saved.
856 * @nbr Real number of network interfaces available.
859 * @st_net_dev Structure with statistics.
860 ***************************************************************************
862 void read_if_info(struct stats_net_dev *st_net_dev, int nbr)
865 struct stats_net_dev *st_net_dev_i;
866 char filename[128], duplex[32];
869 for (dev = 0; dev < nbr; dev++) {
871 st_net_dev_i = st_net_dev + dev;
873 /* Read speed info */
874 sprintf(filename, IF_DUPLEX, st_net_dev_i->interface);
876 if ((fp = fopen(filename, "r")) == NULL)
877 /* Cannot read NIC duplex */
880 n = fscanf(fp, "%31s", duplex);
885 /* Cannot read NIC duplex */
888 if (!strcmp(duplex, K_DUPLEX_FULL)) {
889 st_net_dev_i->duplex = C_DUPLEX_FULL;
891 else if (!strcmp(duplex, K_DUPLEX_HALF)) {
892 st_net_dev_i->duplex = C_DUPLEX_HALF;
897 /* Read speed info */
898 sprintf(filename, IF_SPEED, st_net_dev_i->interface);
900 if ((fp = fopen(filename, "r")) == NULL)
901 /* Cannot read NIC speed */
904 n = fscanf(fp, "%u", &st_net_dev_i->speed);
909 st_net_dev_i->speed = 0;
916 ***************************************************************************
917 * Read network interfaces errors statistics from /proc/net/dev.
920 * @st_net_edev Structure where stats will be saved.
921 * @nbr Maximum number of network interfaces.
924 * @st_net_edev Structure with statistics.
925 ***************************************************************************
927 void read_net_edev(struct stats_net_edev *st_net_edev, int nbr)
930 struct stats_net_edev *st_net_edev_i;
931 static char line[256];
932 char iface[MAX_IFACE_LEN];
936 if ((fp = fopen(NET_DEV, "r")) == NULL)
939 while ((fgets(line, sizeof(line), fp) != NULL) && (dev < nbr)) {
941 pos = strcspn(line, ":");
942 if (pos < strlen(line)) {
943 st_net_edev_i = st_net_edev + dev;
944 strncpy(iface, line, MINIMUM(pos, MAX_IFACE_LEN - 1));
945 iface[MINIMUM(pos, MAX_IFACE_LEN - 1)] = '\0';
946 sscanf(iface, "%s", st_net_edev_i->interface); /* Skip heading spaces */
947 sscanf(line + pos + 1, "%*u %*u %llu %llu %llu %llu %*u %*u %*u %*u "
948 "%llu %llu %llu %llu %llu",
949 &st_net_edev_i->rx_errors,
950 &st_net_edev_i->rx_dropped,
951 &st_net_edev_i->rx_fifo_errors,
952 &st_net_edev_i->rx_frame_errors,
953 &st_net_edev_i->tx_errors,
954 &st_net_edev_i->tx_dropped,
955 &st_net_edev_i->tx_fifo_errors,
956 &st_net_edev_i->collisions,
957 &st_net_edev_i->tx_carrier_errors);
966 ***************************************************************************
967 * Read NFS client statistics from /proc/net/rpc/nfs.
970 * @st_net_nfs Structure where stats will be saved.
973 * @st_net_nfs Structure with statistics.
974 ***************************************************************************
976 void read_net_nfs(struct stats_net_nfs *st_net_nfs)
980 unsigned int getattcnt = 0, accesscnt = 0, readcnt = 0, writecnt = 0;
982 if ((fp = fopen(NET_RPC_NFS, "r")) == NULL)
985 memset(st_net_nfs, 0, STATS_NET_NFS_SIZE);
987 while (fgets(line, sizeof(line), fp) != NULL) {
989 if (!strncmp(line, "rpc ", 4)) {
990 sscanf(line + 4, "%u %u",
991 &st_net_nfs->nfs_rpccnt, &st_net_nfs->nfs_rpcretrans);
993 else if (!strncmp(line, "proc3 ", 6)) {
994 sscanf(line + 6, "%*u %*u %u %*u %*u %u %*u %u %u",
995 &getattcnt, &accesscnt, &readcnt, &writecnt);
997 st_net_nfs->nfs_getattcnt += getattcnt;
998 st_net_nfs->nfs_accesscnt += accesscnt;
999 st_net_nfs->nfs_readcnt += readcnt;
1000 st_net_nfs->nfs_writecnt += writecnt;
1002 else if (!strncmp(line, "proc4 ", 6)) {
1003 sscanf(line + 6, "%*u %*u %u %u "
1004 "%*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %u %u",
1005 &readcnt, &writecnt, &accesscnt, &getattcnt);
1007 st_net_nfs->nfs_getattcnt += getattcnt;
1008 st_net_nfs->nfs_accesscnt += accesscnt;
1009 st_net_nfs->nfs_readcnt += readcnt;
1010 st_net_nfs->nfs_writecnt += writecnt;
1018 ***************************************************************************
1019 * Read NFS server statistics from /proc/net/rpc/nfsd.
1022 * @st_net_nfsd Structure where stats will be saved.
1025 * @st_net_nfsd Structure with statistics.
1026 ***************************************************************************
1028 void read_net_nfsd(struct stats_net_nfsd *st_net_nfsd)
1032 unsigned int getattcnt = 0, accesscnt = 0, readcnt = 0, writecnt = 0;
1034 if ((fp = fopen(NET_RPC_NFSD, "r")) == NULL)
1037 memset(st_net_nfsd, 0, STATS_NET_NFSD_SIZE);
1039 while (fgets(line, sizeof(line), fp) != NULL) {
1041 if (!strncmp(line, "rc ", 3)) {
1042 sscanf(line + 3, "%u %u",
1043 &st_net_nfsd->nfsd_rchits, &st_net_nfsd->nfsd_rcmisses);
1045 else if (!strncmp(line, "net ", 4)) {
1046 sscanf(line + 4, "%u %u %u",
1047 &st_net_nfsd->nfsd_netcnt, &st_net_nfsd->nfsd_netudpcnt,
1048 &st_net_nfsd->nfsd_nettcpcnt);
1050 else if (!strncmp(line, "rpc ", 4)) {
1051 sscanf(line + 4, "%u %u",
1052 &st_net_nfsd->nfsd_rpccnt, &st_net_nfsd->nfsd_rpcbad);
1054 else if (!strncmp(line, "proc3 ", 6)) {
1055 sscanf(line + 6, "%*u %*u %u %*u %*u %u %*u %u %u",
1056 &getattcnt, &accesscnt, &readcnt, &writecnt);
1058 st_net_nfsd->nfsd_getattcnt += getattcnt;
1059 st_net_nfsd->nfsd_accesscnt += accesscnt;
1060 st_net_nfsd->nfsd_readcnt += readcnt;
1061 st_net_nfsd->nfsd_writecnt += writecnt;
1064 else if (!strncmp(line, "proc4ops ", 9)) {
1065 sscanf(line + 9, "%*u %*u %*u %*u %u "
1066 "%*u %*u %*u %*u %*u %u "
1067 "%*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %u "
1068 "%*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %u",
1069 &accesscnt, &getattcnt, &readcnt, &writecnt);
1071 st_net_nfsd->nfsd_getattcnt += getattcnt;
1072 st_net_nfsd->nfsd_accesscnt += accesscnt;
1073 st_net_nfsd->nfsd_readcnt += readcnt;
1074 st_net_nfsd->nfsd_writecnt += writecnt;
1082 ***************************************************************************
1083 * Read network sockets statistics from /proc/net/sockstat.
1086 * @st_net_sock Structure where stats will be saved.
1089 * @st_net_sock Structure with statistics.
1090 ***************************************************************************
1092 void read_net_sock(struct stats_net_sock *st_net_sock)
1098 if ((fp = fopen(NET_SOCKSTAT, "r")) == NULL)
1101 while (fgets(line, sizeof(line), fp) != NULL) {
1103 if (!strncmp(line, "sockets:", 8)) {
1105 sscanf(line + 14, "%u", &st_net_sock->sock_inuse);
1107 else if (!strncmp(line, "TCP:", 4)) {
1109 sscanf(line + 11, "%u", &st_net_sock->tcp_inuse);
1110 if ((p = strstr(line, "tw")) != NULL) {
1111 sscanf(p + 2, "%u", &st_net_sock->tcp_tw);
1114 else if (!strncmp(line, "UDP:", 4)) {
1116 sscanf(line + 11, "%u", &st_net_sock->udp_inuse);
1118 else if (!strncmp(line, "RAW:", 4)) {
1120 sscanf(line + 11, "%u", &st_net_sock->raw_inuse);
1122 else if (!strncmp(line, "FRAG:", 5)) {
1124 sscanf(line + 12, "%u", &st_net_sock->frag_inuse);
1132 ***************************************************************************
1133 * Read IP network traffic statistics from /proc/net/snmp.
1136 * @st_net_ip Structure where stats will be saved.
1139 * @st_net_ip Structure with statistics.
1140 ***************************************************************************
1142 void read_net_ip(struct stats_net_ip *st_net_ip)
1148 if ((fp = fopen(NET_SNMP, "r")) == NULL)
1151 while (fgets(line, sizeof(line), fp) != NULL) {
1153 if (!strncmp(line, "Ip:", 3)) {
1155 sscanf(line + 3, "%*u %*u %llu %*u %*u %llu %*u %*u "
1156 "%llu %llu %*u %*u %*u %llu %llu %*u %llu %*u %llu",
1157 &st_net_ip->InReceives,
1158 &st_net_ip->ForwDatagrams,
1159 &st_net_ip->InDelivers,
1160 &st_net_ip->OutRequests,
1161 &st_net_ip->ReasmReqds,
1162 &st_net_ip->ReasmOKs,
1163 &st_net_ip->FragOKs,
1164 &st_net_ip->FragCreates);
1178 ***************************************************************************
1179 * Read IP network errors statistics from /proc/net/snmp.
1182 * @st_net_eip Structure where stats will be saved.
1185 * @st_net_eip Structure with statistics.
1186 ***************************************************************************
1188 void read_net_eip(struct stats_net_eip *st_net_eip)
1194 if ((fp = fopen(NET_SNMP, "r")) == NULL)
1197 while (fgets(line, sizeof(line), fp) != NULL) {
1199 if (!strncmp(line, "Ip:", 3)) {
1201 sscanf(line + 3, "%*u %*u %*u %llu %llu %*u %llu %llu "
1202 "%*u %*u %llu %llu %*u %*u %*u %llu %*u %llu",
1203 &st_net_eip->InHdrErrors,
1204 &st_net_eip->InAddrErrors,
1205 &st_net_eip->InUnknownProtos,
1206 &st_net_eip->InDiscards,
1207 &st_net_eip->OutDiscards,
1208 &st_net_eip->OutNoRoutes,
1209 &st_net_eip->ReasmFails,
1210 &st_net_eip->FragFails);
1224 ***************************************************************************
1225 * Read ICMP network traffic statistics from /proc/net/snmp.
1228 * @st_net_icmp Structure where stats will be saved.
1231 * @st_net_icmp Structure with statistics.
1232 ***************************************************************************
1234 void read_net_icmp(struct stats_net_icmp *st_net_icmp)
1238 static char format[256] = "";
1241 if ((fp = fopen(NET_SNMP, "r")) == NULL)
1244 while (fgets(line, sizeof(line), fp) != NULL) {
1246 if (!strncmp(line, "Icmp:", 5)) {
1248 sscanf(line + 5, format,
1249 &st_net_icmp->InMsgs,
1250 &st_net_icmp->InEchos,
1251 &st_net_icmp->InEchoReps,
1252 &st_net_icmp->InTimestamps,
1253 &st_net_icmp->InTimestampReps,
1254 &st_net_icmp->InAddrMasks,
1255 &st_net_icmp->InAddrMaskReps,
1256 &st_net_icmp->OutMsgs,
1257 &st_net_icmp->OutEchos,
1258 &st_net_icmp->OutEchoReps,
1259 &st_net_icmp->OutTimestamps,
1260 &st_net_icmp->OutTimestampReps,
1261 &st_net_icmp->OutAddrMasks,
1262 &st_net_icmp->OutAddrMaskReps);
1267 if (!strlen(format)) {
1268 if (strstr(line, "InCsumErrors")) {
1270 * New format: InCsumErrors field exists at position #3.
1271 * Capture: 1,9,10,11,12,13,14,15,22,23,24,25,26,27.
1273 strcpy(format, "%lu %*u %*u %*u %*u %*u %*u %*u "
1274 "%lu %lu %lu %lu %lu %lu %lu %*u %*u %*u %*u "
1275 "%*u %*u %lu %lu %lu %lu %lu %lu");
1279 * Old format: InCsumErrors field doesn't exist.
1280 * Capture: 1,8,9,10,11,12,13,14,21,22,23,24,25,26.
1282 strcpy(format, "%lu %*u %*u %*u %*u %*u %*u "
1283 "%lu %lu %lu %lu %lu %lu %lu %*u %*u %*u %*u "
1284 "%*u %*u %lu %lu %lu %lu %lu %lu");
1296 ***************************************************************************
1297 * Read ICMP network errors statistics from /proc/net/snmp.
1300 * @st_net_eicmp Structure where stats will be saved.
1303 * @st_net_eicmp Structure with statistics.
1304 ***************************************************************************
1306 void read_net_eicmp(struct stats_net_eicmp *st_net_eicmp)
1312 if ((fp = fopen(NET_SNMP, "r")) == NULL)
1315 while (fgets(line, sizeof(line), fp) != NULL) {
1317 if (!strncmp(line, "Icmp:", 5)) {
1319 sscanf(line + 5, "%*u %lu %lu %lu %lu %lu %lu %*u %*u "
1320 "%*u %*u %*u %*u %*u %lu %lu %lu %lu %lu %lu",
1321 &st_net_eicmp->InErrors,
1322 &st_net_eicmp->InDestUnreachs,
1323 &st_net_eicmp->InTimeExcds,
1324 &st_net_eicmp->InParmProbs,
1325 &st_net_eicmp->InSrcQuenchs,
1326 &st_net_eicmp->InRedirects,
1327 &st_net_eicmp->OutErrors,
1328 &st_net_eicmp->OutDestUnreachs,
1329 &st_net_eicmp->OutTimeExcds,
1330 &st_net_eicmp->OutParmProbs,
1331 &st_net_eicmp->OutSrcQuenchs,
1332 &st_net_eicmp->OutRedirects);
1346 ***************************************************************************
1347 * Read TCP network traffic statistics from /proc/net/snmp.
1350 * @st_net_tcp Structure where stats will be saved.
1353 * @st_net_tcp Structure with statistics.
1354 ***************************************************************************
1356 void read_net_tcp(struct stats_net_tcp *st_net_tcp)
1362 if ((fp = fopen(NET_SNMP, "r")) == NULL)
1365 while (fgets(line, sizeof(line), fp) != NULL) {
1367 if (!strncmp(line, "Tcp:", 4)) {
1369 sscanf(line + 4, "%*u %*u %*u %*d %lu %lu "
1370 "%*u %*u %*u %lu %lu",
1371 &st_net_tcp->ActiveOpens,
1372 &st_net_tcp->PassiveOpens,
1373 &st_net_tcp->InSegs,
1374 &st_net_tcp->OutSegs);
1388 ***************************************************************************
1389 * Read TCP network errors statistics from /proc/net/snmp.
1392 * @st_net_etcp Structure where stats will be saved.
1395 * @st_net_etcp Structure with statistics.
1396 ***************************************************************************
1398 void read_net_etcp(struct stats_net_etcp *st_net_etcp)
1404 if ((fp = fopen(NET_SNMP, "r")) == NULL)
1407 while (fgets(line, sizeof(line), fp) != NULL) {
1409 if (!strncmp(line, "Tcp:", 4)) {
1411 sscanf(line + 4, "%*u %*u %*u %*d %*u %*u "
1412 "%lu %lu %*u %*u %*u %lu %lu %lu",
1413 &st_net_etcp->AttemptFails,
1414 &st_net_etcp->EstabResets,
1415 &st_net_etcp->RetransSegs,
1416 &st_net_etcp->InErrs,
1417 &st_net_etcp->OutRsts);
1431 ***************************************************************************
1432 * Read UDP network traffic statistics from /proc/net/snmp.
1435 * @st_net_udp Structure where stats will be saved.
1438 * @st_net_udp Structure with statistics.
1439 ***************************************************************************
1441 void read_net_udp(struct stats_net_udp *st_net_udp)
1447 if ((fp = fopen(NET_SNMP, "r")) == NULL)
1450 while (fgets(line, sizeof(line), fp) != NULL) {
1452 if (!strncmp(line, "Udp:", 4)) {
1454 sscanf(line + 4, "%lu %lu %lu %lu",
1455 &st_net_udp->InDatagrams,
1456 &st_net_udp->NoPorts,
1457 &st_net_udp->InErrors,
1458 &st_net_udp->OutDatagrams);
1472 ***************************************************************************
1473 * Read IPv6 network sockets statistics from /proc/net/sockstat6.
1476 * @st_net_sock6 Structure where stats will be saved.
1479 * @st_net_sock6 Structure with statistics.
1480 ***************************************************************************
1482 void read_net_sock6(struct stats_net_sock6 *st_net_sock6)
1487 if ((fp = fopen(NET_SOCKSTAT6, "r")) == NULL)
1490 while (fgets(line, sizeof(line), fp) != NULL) {
1492 if (!strncmp(line, "TCP6:", 5)) {
1494 sscanf(line + 12, "%u", &st_net_sock6->tcp6_inuse);
1496 else if (!strncmp(line, "UDP6:", 5)) {
1498 sscanf(line + 12, "%u", &st_net_sock6->udp6_inuse);
1500 else if (!strncmp(line, "RAW6:", 5)) {
1501 /* IPv6 RAW sockets */
1502 sscanf(line + 12, "%u", &st_net_sock6->raw6_inuse);
1504 else if (!strncmp(line, "FRAG6:", 6)) {
1505 /* IPv6 FRAGments */
1506 sscanf(line + 13, "%u", &st_net_sock6->frag6_inuse);
1514 ***************************************************************************
1515 * Read IPv6 network traffic statistics from /proc/net/snmp6.
1518 * @st_net_ip6 Structure where stats will be saved.
1521 * @st_net_ip6 Structure with statistics.
1522 ***************************************************************************
1524 void read_net_ip6(struct stats_net_ip6 *st_net_ip6)
1529 if ((fp = fopen(NET_SNMP6, "r")) == NULL)
1532 while (fgets(line, sizeof(line), fp) != NULL) {
1534 if (!strncmp(line, "Ip6InReceives ", 14)) {
1535 sscanf(line + 14, "%llu", &st_net_ip6->InReceives6);
1537 else if (!strncmp(line, "Ip6OutForwDatagrams ", 20)) {
1538 sscanf(line + 20, "%llu", &st_net_ip6->OutForwDatagrams6);
1540 else if (!strncmp(line, "Ip6InDelivers ", 14)) {
1541 sscanf(line + 14, "%llu", &st_net_ip6->InDelivers6);
1543 else if (!strncmp(line, "Ip6OutRequests ", 15)) {
1544 sscanf(line + 15, "%llu", &st_net_ip6->OutRequests6);
1546 else if (!strncmp(line, "Ip6ReasmReqds ", 14)) {
1547 sscanf(line + 14, "%llu", &st_net_ip6->ReasmReqds6);
1549 else if (!strncmp(line, "Ip6ReasmOKs ", 12)) {
1550 sscanf(line + 12, "%llu", &st_net_ip6->ReasmOKs6);
1552 else if (!strncmp(line, "Ip6InMcastPkts ", 15)) {
1553 sscanf(line + 15, "%llu", &st_net_ip6->InMcastPkts6);
1555 else if (!strncmp(line, "Ip6OutMcastPkts ", 16)) {
1556 sscanf(line + 16, "%llu", &st_net_ip6->OutMcastPkts6);
1558 else if (!strncmp(line, "Ip6FragOKs ", 11)) {
1559 sscanf(line + 11, "%llu", &st_net_ip6->FragOKs6);
1561 else if (!strncmp(line, "Ip6FragCreates ", 15)) {
1562 sscanf(line + 15, "%llu", &st_net_ip6->FragCreates6);
1570 ***************************************************************************
1571 * Read IPv6 network errors statistics from /proc/net/snmp6.
1574 * @st_net_eip6 Structure where stats will be saved.
1577 * @st_net_eip6 Structure with statistics.
1578 ***************************************************************************
1580 void read_net_eip6(struct stats_net_eip6 *st_net_eip6)
1585 if ((fp = fopen(NET_SNMP6, "r")) == NULL)
1588 while (fgets(line, sizeof(line), fp) != NULL) {
1590 if (!strncmp(line, "Ip6InHdrErrors ", 15)) {
1591 sscanf(line + 15, "%llu", &st_net_eip6->InHdrErrors6);
1593 else if (!strncmp(line, "Ip6InAddrErrors ", 16)) {
1594 sscanf(line + 16, "%llu", &st_net_eip6->InAddrErrors6);
1596 else if (!strncmp(line, "Ip6InUnknownProtos ", 19)) {
1597 sscanf(line + 19, "%llu", &st_net_eip6->InUnknownProtos6);
1599 else if (!strncmp(line, "Ip6InTooBigErrors ", 18)) {
1600 sscanf(line + 18, "%llu", &st_net_eip6->InTooBigErrors6);
1602 else if (!strncmp(line, "Ip6InDiscards ", 14)) {
1603 sscanf(line + 14, "%llu", &st_net_eip6->InDiscards6);
1605 else if (!strncmp(line, "Ip6OutDiscards ", 15)) {
1606 sscanf(line + 15, "%llu", &st_net_eip6->OutDiscards6);
1608 else if (!strncmp(line, "Ip6InNoRoutes ", 14)) {
1609 sscanf(line + 14, "%llu", &st_net_eip6->InNoRoutes6);
1611 else if (!strncmp(line, "Ip6OutNoRoutes ", 15)) {
1612 sscanf(line + 15, "%llu", &st_net_eip6->OutNoRoutes6);
1614 else if (!strncmp(line, "Ip6ReasmFails ", 14)) {
1615 sscanf(line + 14, "%llu", &st_net_eip6->ReasmFails6);
1617 else if (!strncmp(line, "Ip6FragFails ", 13)) {
1618 sscanf(line + 13, "%llu", &st_net_eip6->FragFails6);
1620 else if (!strncmp(line, "Ip6InTruncatedPkts ", 19)) {
1621 sscanf(line + 19, "%llu", &st_net_eip6->InTruncatedPkts6);
1629 ***************************************************************************
1630 * Read ICMPv6 network traffic statistics from /proc/net/snmp6.
1633 * @st_net_icmp6 Structure where stats will be saved.
1636 * @st_net_icmp6 Structure with statistics.
1637 ***************************************************************************
1639 void read_net_icmp6(struct stats_net_icmp6 *st_net_icmp6)
1644 if ((fp = fopen(NET_SNMP6, "r")) == NULL)
1647 while (fgets(line, sizeof(line), fp) != NULL) {
1649 if (!strncmp(line, "Icmp6InMsgs ", 12)) {
1650 sscanf(line + 12, "%lu", &st_net_icmp6->InMsgs6);
1652 else if (!strncmp(line, "Icmp6OutMsgs ", 13)) {
1653 sscanf(line + 13, "%lu", &st_net_icmp6->OutMsgs6);
1655 else if (!strncmp(line, "Icmp6InEchos ", 13)) {
1656 sscanf(line + 13, "%lu", &st_net_icmp6->InEchos6);
1658 else if (!strncmp(line, "Icmp6InEchoReplies ", 19)) {
1659 sscanf(line + 19, "%lu", &st_net_icmp6->InEchoReplies6);
1661 else if (!strncmp(line, "Icmp6OutEchoReplies ", 20)) {
1662 sscanf(line + 20, "%lu", &st_net_icmp6->OutEchoReplies6);
1664 else if (!strncmp(line, "Icmp6InGroupMembQueries ", 24)) {
1665 sscanf(line + 24, "%lu", &st_net_icmp6->InGroupMembQueries6);
1667 else if (!strncmp(line, "Icmp6InGroupMembResponses ", 26)) {
1668 sscanf(line + 26, "%lu", &st_net_icmp6->InGroupMembResponses6);
1670 else if (!strncmp(line, "Icmp6OutGroupMembResponses ", 27)) {
1671 sscanf(line + 27, "%lu", &st_net_icmp6->OutGroupMembResponses6);
1673 else if (!strncmp(line, "Icmp6InGroupMembReductions ", 27)) {
1674 sscanf(line + 27, "%lu", &st_net_icmp6->InGroupMembReductions6);
1676 else if (!strncmp(line, "Icmp6OutGroupMembReductions ", 28)) {
1677 sscanf(line + 28, "%lu", &st_net_icmp6->OutGroupMembReductions6);
1679 else if (!strncmp(line, "Icmp6InRouterSolicits ", 22)) {
1680 sscanf(line + 22, "%lu", &st_net_icmp6->InRouterSolicits6);
1682 else if (!strncmp(line, "Icmp6OutRouterSolicits ", 23)) {
1683 sscanf(line + 23, "%lu", &st_net_icmp6->OutRouterSolicits6);
1685 else if (!strncmp(line, "Icmp6InRouterAdvertisements ", 28)) {
1686 sscanf(line + 28, "%lu", &st_net_icmp6->InRouterAdvertisements6);
1688 else if (!strncmp(line, "Icmp6InNeighborSolicits ", 24)) {
1689 sscanf(line + 24, "%lu", &st_net_icmp6->InNeighborSolicits6);
1691 else if (!strncmp(line, "Icmp6OutNeighborSolicits ", 25)) {
1692 sscanf(line + 25, "%lu", &st_net_icmp6->OutNeighborSolicits6);
1694 else if (!strncmp(line, "Icmp6InNeighborAdvertisements ", 30)) {
1695 sscanf(line + 30, "%lu", &st_net_icmp6->InNeighborAdvertisements6);
1697 else if (!strncmp(line, "Icmp6OutNeighborAdvertisements ", 31)) {
1698 sscanf(line + 31, "%lu", &st_net_icmp6->OutNeighborAdvertisements6);
1706 ***************************************************************************
1707 * Read ICMPv6 network errors statistics from /proc/net/snmp6.
1710 * @st_net_eicmp6 Structure where stats will be saved.
1713 * @st_net_eicmp6 Structure with statistics.
1714 ***************************************************************************
1716 void read_net_eicmp6(struct stats_net_eicmp6 *st_net_eicmp6)
1721 if ((fp = fopen(NET_SNMP6, "r")) == NULL)
1724 while (fgets(line, sizeof(line), fp) != NULL) {
1726 if (!strncmp(line, "Icmp6InErrors ", 14)) {
1727 sscanf(line + 14, "%lu", &st_net_eicmp6->InErrors6);
1729 else if (!strncmp(line, "Icmp6InDestUnreachs ", 20)) {
1730 sscanf(line + 20, "%lu", &st_net_eicmp6->InDestUnreachs6);
1732 else if (!strncmp(line, "Icmp6OutDestUnreachs ", 21)) {
1733 sscanf(line + 21, "%lu", &st_net_eicmp6->OutDestUnreachs6);
1735 else if (!strncmp(line, "Icmp6InTimeExcds ", 17)) {
1736 sscanf(line + 17, "%lu", &st_net_eicmp6->InTimeExcds6);
1738 else if (!strncmp(line, "Icmp6OutTimeExcds ", 18)) {
1739 sscanf(line + 18, "%lu", &st_net_eicmp6->OutTimeExcds6);
1741 else if (!strncmp(line, "Icmp6InParmProblems ", 20)) {
1742 sscanf(line + 20, "%lu", &st_net_eicmp6->InParmProblems6);
1744 else if (!strncmp(line, "Icmp6OutParmProblems ", 21)) {
1745 sscanf(line + 21, "%lu", &st_net_eicmp6->OutParmProblems6);
1747 else if (!strncmp(line, "Icmp6InRedirects ", 17)) {
1748 sscanf(line + 17, "%lu", &st_net_eicmp6->InRedirects6);
1750 else if (!strncmp(line, "Icmp6OutRedirects ", 18)) {
1751 sscanf(line + 18, "%lu", &st_net_eicmp6->OutRedirects6);
1753 else if (!strncmp(line, "Icmp6InPktTooBigs ", 18)) {
1754 sscanf(line + 18, "%lu", &st_net_eicmp6->InPktTooBigs6);
1756 else if (!strncmp(line, "Icmp6OutPktTooBigs ", 19)) {
1757 sscanf(line + 19, "%lu", &st_net_eicmp6->OutPktTooBigs6);
1765 ***************************************************************************
1766 * Read UDPv6 network traffic statistics from /proc/net/snmp6.
1769 * @st_net_udp6 Structure where stats will be saved.
1772 * @st_net_udp6 Structure with statistics.
1773 ***************************************************************************
1775 void read_net_udp6(struct stats_net_udp6 *st_net_udp6)
1780 if ((fp = fopen(NET_SNMP6, "r")) == NULL)
1783 while (fgets(line, sizeof(line), fp) != NULL) {
1785 if (!strncmp(line, "Udp6InDatagrams ", 16)) {
1786 sscanf(line + 16, "%lu", &st_net_udp6->InDatagrams6);
1788 else if (!strncmp(line, "Udp6OutDatagrams ", 17)) {
1789 sscanf(line + 17, "%lu", &st_net_udp6->OutDatagrams6);
1791 else if (!strncmp(line, "Udp6NoPorts ", 12)) {
1792 sscanf(line + 12, "%lu", &st_net_udp6->NoPorts6);
1794 else if (!strncmp(line, "Udp6InErrors ", 13)) {
1795 sscanf(line + 13, "%lu", &st_net_udp6->InErrors6);
1803 ***************************************************************************
1804 * Read CPU frequency statistics.
1807 * @st_pwr_cpufreq Structure where stats will be saved.
1808 * @nbr Total number of CPU (including cpu "all").
1811 * @st_pwr_cpufreq Structure with statistics.
1812 ***************************************************************************
1814 void read_cpuinfo(struct stats_pwr_cpufreq *st_pwr_cpufreq, int nbr)
1817 struct stats_pwr_cpufreq *st_pwr_cpufreq_i;
1820 unsigned int proc_nb = 0, ifreq, dfreq;
1822 if ((fp = fopen(CPUINFO, "r")) == NULL)
1825 st_pwr_cpufreq->cpufreq = 0;
1827 while (fgets(line, sizeof(line), fp) != NULL) {
1829 if (!strncmp(line, "processor\t", 10)) {
1830 sscanf(strchr(line, ':') + 1, "%u", &proc_nb);
1833 /* Entry in /proc/cpuinfo is different between Intel and Power architectures */
1834 else if (!strncmp(line, "cpu MHz\t", 8) ||
1835 !strncmp(line, "clock\t", 6)) {
1836 sscanf(strchr(line, ':') + 1, "%u.%u", &ifreq, &dfreq);
1838 if (proc_nb < (nbr - 1)) {
1839 /* Save current CPU frequency */
1840 st_pwr_cpufreq_i = st_pwr_cpufreq + proc_nb + 1;
1841 st_pwr_cpufreq_i->cpufreq = ifreq * 100 + dfreq / 10;
1843 /* Also save it to compute an average CPU frequency */
1844 st_pwr_cpufreq->cpufreq += st_pwr_cpufreq_i->cpufreq;
1847 else if (!proc_nb && (nbr == 1)) {
1849 * We are reading freq for "Processor 0" and we have a machine
1850 * with only one processor and not an SMP kernel, with /sys not mounted
1851 * (the nr of proc has been counted using /proc/stat and there was
1852 * only one line with global CPU stats here).
1853 * This is a very specific case, I must admit...
1855 st_pwr_cpufreq->cpufreq = ifreq * 100 + dfreq / 10;
1863 /* Compute average CPU frequency for this machine */
1864 st_pwr_cpufreq->cpufreq /= nr;
1869 ***************************************************************************
1870 * Read hugepages statistics from /proc/meminfo.
1873 * @st_huge Structure where stats will be saved.
1876 * @st_huge Structure with statistics.
1877 ***************************************************************************
1879 void read_meminfo_huge(struct stats_huge *st_huge)
1883 unsigned long szhkb = 0;
1885 if ((fp = fopen(MEMINFO, "r")) == NULL)
1888 while (fgets(line, sizeof(line), fp) != NULL) {
1890 if (!strncmp(line, "HugePages_Total:", 16)) {
1891 /* Read the total number of huge pages */
1892 sscanf(line + 16, "%lu", &st_huge->tlhkb);
1894 else if (!strncmp(line, "HugePages_Free:", 15)) {
1895 /* Read the number of free huge pages */
1896 sscanf(line + 15, "%lu", &st_huge->frhkb);
1898 else if (!strncmp(line, "Hugepagesize:", 13)) {
1899 /* Read the default size of a huge page in kB */
1900 sscanf(line + 13, "%lu", &szhkb);
1906 /* We want huge pages stats in kB and not expressed in a number of pages */
1907 st_huge->tlhkb *= szhkb;
1908 st_huge->frhkb *= szhkb;
1912 ***************************************************************************
1913 * Read CPU average frequencies statistics.
1916 * @st_pwr_wghfreq Structure where stats will be saved.
1917 * @cpu_nr CPU number for which time_in_state date will be read.
1918 * @nbr Total number of states (frequencies).
1921 * @st_pwr_wghfreq Structure with statistics.
1922 ***************************************************************************
1924 void read_time_in_state(struct stats_pwr_wghfreq *st_pwr_wghfreq, int cpu_nr, int nbr)
1927 struct stats_pwr_wghfreq *st_pwr_wghfreq_j;
1928 char filename[MAX_PF_NAME];
1932 unsigned long long time_in_state;
1934 snprintf(filename, MAX_PF_NAME, "%s/cpu%d/%s",
1935 SYSFS_DEVCPU, cpu_nr, SYSFS_TIME_IN_STATE);
1936 if ((fp = fopen(filename, "r")) == NULL)
1939 while (fgets(line, sizeof(line), fp) != NULL) {
1941 sscanf(line, "%lu %llu", &freq, &time_in_state);
1944 /* Save current frequency and time */
1945 st_pwr_wghfreq_j = st_pwr_wghfreq + j;
1946 st_pwr_wghfreq_j->freq = freq;
1947 st_pwr_wghfreq_j->time_in_state = time_in_state;
1956 ***************************************************************************
1957 * Read current USB device data.
1960 * @st_pwr_usb Structure where stats will be saved.
1961 * @usb_device File name for current USB device.
1964 * @st_pwr_usb Structure with statistics.
1965 ***************************************************************************
1967 void read_usb_stats(struct stats_pwr_usb *st_pwr_usb, char *usb_device)
1972 char filename[MAX_PF_NAME];
1974 /* Get USB device bus number */
1975 sscanf(usb_device, "%u", &st_pwr_usb->bus_nr);
1977 /* Read USB device vendor ID */
1978 snprintf(filename, MAX_PF_NAME, "%s/%s/%s",
1979 SYSFS_USBDEV, usb_device, SYSFS_IDVENDOR);
1980 if ((fp = fopen(filename, "r")) != NULL) {
1981 rc = fscanf(fp, "%x",
1982 &st_pwr_usb->vendor_id);
1985 st_pwr_usb->vendor_id = 0;
1989 /* Read USB device product ID */
1990 snprintf(filename, MAX_PF_NAME, "%s/%s/%s",
1991 SYSFS_USBDEV, usb_device, SYSFS_IDPRODUCT);
1992 if ((fp = fopen(filename, "r")) != NULL) {
1993 rc = fscanf(fp, "%x",
1994 &st_pwr_usb->product_id);
1997 st_pwr_usb->product_id = 0;
2001 /* Read USB device max power consumption */
2002 snprintf(filename, MAX_PF_NAME, "%s/%s/%s",
2003 SYSFS_USBDEV, usb_device, SYSFS_BMAXPOWER);
2004 if ((fp = fopen(filename, "r")) != NULL) {
2005 rc = fscanf(fp, "%u",
2006 &st_pwr_usb->bmaxpower);
2009 st_pwr_usb->bmaxpower = 0;
2013 /* Read USB device manufacturer */
2014 snprintf(filename, MAX_PF_NAME, "%s/%s/%s",
2015 SYSFS_USBDEV, usb_device, SYSFS_MANUFACTURER);
2016 if ((fp = fopen(filename, "r")) != NULL) {
2017 rs = fgets(st_pwr_usb->manufacturer,
2018 MAX_MANUF_LEN - 1, fp);
2021 (l = strlen(st_pwr_usb->manufacturer)) > 0) {
2022 /* Remove trailing CR */
2023 st_pwr_usb->manufacturer[l - 1] = '\0';
2027 /* Read USB device product */
2028 snprintf(filename, MAX_PF_NAME, "%s/%s/%s",
2029 SYSFS_USBDEV, usb_device, SYSFS_PRODUCT);
2030 if ((fp = fopen(filename, "r")) != NULL) {
2031 rs = fgets(st_pwr_usb->product,
2032 MAX_PROD_LEN - 1, fp);
2035 (l = strlen(st_pwr_usb->product)) > 0) {
2036 /* Remove trailing CR */
2037 st_pwr_usb->product[l - 1] = '\0';
2043 ***************************************************************************
2044 * Read USB devices statistics.
2047 * @st_pwr_usb Structure where stats will be saved.
2048 * @nbr Total number of USB devices.
2051 * @st_pwr_usb Structure with statistics.
2052 ***************************************************************************
2054 void read_bus_usb_dev(struct stats_pwr_usb *st_pwr_usb, int nbr)
2058 struct stats_pwr_usb *st_pwr_usb_j;
2061 /* Open relevant /sys directory */
2062 if ((dir = opendir(SYSFS_USBDEV)) == NULL)
2065 /* Get current file entry */
2066 while ((drd = readdir(dir)) != NULL) {
2068 if (isdigit(drd->d_name[0]) && !strchr(drd->d_name, ':')) {
2070 /* Read current USB device data */
2071 st_pwr_usb_j = st_pwr_usb + j;
2072 read_usb_stats(st_pwr_usb_j, drd->d_name);
2080 /* Close directory */
2085 ***************************************************************************
2086 * Read filesystems statistics.
2089 * @st_filesystem Structure where stats will be saved.
2090 * @nbr Total number of filesystems.
2093 * @st_filesystem Structure with statistics.
2094 ***************************************************************************
2096 void read_filesystem(struct stats_filesystem *st_filesystem, int nbr)
2099 char line[512], fs_name[128], mountp[256];
2101 struct stats_filesystem *st_filesystem_i;
2104 if ((fp = fopen(MTAB, "r")) == NULL)
2107 while ((fgets(line, sizeof(line), fp) != NULL) && (fs < nbr)) {
2108 if (line[0] == '/') {
2110 /* Read current filesystem name */
2111 sscanf(line, "%127s", fs_name);
2113 * And now read the corresponding mount point.
2114 * Read fs name and mount point in two distinct operations.
2115 * Indeed, if fs name length is greater than 127 chars,
2116 * previous scanf() will read only the first 127 chars, and
2117 * mount point name will be read using the remaining chars
2118 * from the fs name. This will result in a bogus name
2119 * and following statvfs() function will always fail.
2121 sscanf(strchr(line, ' ') + 1, "%255s", mountp);
2123 /* Replace octal codes */
2127 * It's important to have read the whole mount point name
2128 * for statvfs() to work properly (see above).
2130 if ((statvfs(mountp, &buf) < 0) || (!buf.f_blocks))
2133 st_filesystem_i = st_filesystem + fs++;
2134 st_filesystem_i->f_blocks = buf.f_blocks * buf.f_frsize;
2135 st_filesystem_i->f_bfree = buf.f_bfree * buf.f_frsize;
2136 st_filesystem_i->f_bavail = buf.f_bavail * buf.f_frsize;
2137 st_filesystem_i->f_files = buf.f_files;
2138 st_filesystem_i->f_ffree = buf.f_ffree;
2139 strncpy(st_filesystem_i->fs_name, fs_name, MAX_FS_LEN);
2140 st_filesystem_i->fs_name[MAX_FS_LEN - 1] = '\0';
2141 strncpy(st_filesystem_i->mountp, mountp, MAX_FS_LEN);
2142 st_filesystem_i->mountp[MAX_FS_LEN - 1] = '\0';
2150 ***************************************************************************
2151 * Read Fibre Channel HBA statistics.
2154 * @st_fc Structure where stats will be saved.
2155 * @nbr Total number of HBAs.
2158 * @st_fc Structure with statistics.
2159 ***************************************************************************
2161 void read_fchost(struct stats_fchost *st_fc, int nbr)
2166 struct stats_fchost *st_fc_i;
2168 char fcstat_filename[MAX_PF_NAME];
2170 unsigned long rx_frames, tx_frames, rx_words, tx_words;
2172 /* Each host, if present, will have its own hostX entry within SYSFS_FCHOST */
2173 if ((dir = opendir(SYSFS_FCHOST)) == NULL)
2174 return; /* No FC hosts */
2177 * Read each of the counters via sysfs, where they are
2178 * returned as hex values (e.g. 0x72400).
2180 while (((drd = readdir(dir)) != NULL) && (fch < nbr)) {
2181 rx_frames = tx_frames = rx_words = tx_words = 0;
2183 if (!strncmp(drd->d_name, "host", 4)) {
2185 snprintf(fcstat_filename, MAX_PF_NAME, FC_RX_FRAMES,
2186 SYSFS_FCHOST, drd->d_name);
2187 if ((fp = fopen(fcstat_filename, "r"))) {
2188 if (fgets(line, sizeof(line), fp)) {
2189 sscanf(line, "%lx", &rx_frames);
2194 snprintf(fcstat_filename, MAX_PF_NAME, FC_TX_FRAMES,
2195 SYSFS_FCHOST, drd->d_name);
2196 if ((fp = fopen(fcstat_filename, "r"))) {
2197 if (fgets(line, sizeof(line), fp)) {
2198 sscanf(line, "%lx", &tx_frames);
2203 snprintf(fcstat_filename, MAX_PF_NAME, FC_RX_WORDS,
2204 SYSFS_FCHOST, drd->d_name);
2205 if ((fp = fopen(fcstat_filename, "r"))) {
2206 if (fgets(line, sizeof(line), fp)) {
2207 sscanf(line, "%lx", &rx_words);
2212 snprintf(fcstat_filename, MAX_PF_NAME, FC_TX_WORDS,
2213 SYSFS_FCHOST, drd->d_name);
2214 if ((fp = fopen(fcstat_filename, "r"))) {
2215 if (fgets(line, sizeof(line), fp)) {
2216 sscanf(line, "%lx", &tx_words);
2221 st_fc_i = st_fc + fch++;
2222 st_fc_i->f_rxframes = rx_frames;
2223 st_fc_i->f_txframes = tx_frames;
2224 st_fc_i->f_rxwords = rx_words;
2225 st_fc_i->f_txwords = tx_words;
2226 strncpy(st_fc_i->fchost_name, drd->d_name, MAX_FCH_LEN);
2227 st_fc_i->fchost_name[MAX_FCH_LEN - 1] = '\0';
2235 ***************************************************************************
2236 * Read softnet statistics.
2239 * @st_softnet Structure where stats will be saved.
2240 * @nbr Total number of CPU (including cpu "all").
2243 * @st_softnet Structure with statistics.
2244 ***************************************************************************
2246 void read_softnet(struct stats_softnet *st_softnet, int nbr)
2249 struct stats_softnet *st_softnet_i;
2251 unsigned int proc_nb = 1;
2253 /* Open /proc/net/softnet_stat file */
2254 if ((fp = fopen(NET_SOFTNET, "r")) == NULL)
2258 * Init a structure that will contain the values for CPU "all".
2259 * CPU "all" doesn't exist in /proc/net/softnet_stat file, so
2260 * we compute its values as the sum of the values of each CPU.
2262 memset(st_softnet, 0, sizeof(struct stats_softnet));
2264 while ((fgets(line, sizeof(line), fp) != NULL) && (proc_nb < nbr)) {
2266 st_softnet_i = st_softnet + proc_nb++;
2267 sscanf(line, "%x %x %x %*x %*x %*x %*x %*x %*x %x %x",
2268 &st_softnet_i->processed,
2269 &st_softnet_i->dropped,
2270 &st_softnet_i->time_squeeze,
2271 &st_softnet_i->received_rps,
2272 &st_softnet_i->flow_limit);
2274 st_softnet->processed += st_softnet_i->processed;
2275 st_softnet->dropped += st_softnet_i->dropped;
2276 st_softnet->time_squeeze += st_softnet_i->time_squeeze;
2277 st_softnet->received_rps += st_softnet_i->received_rps;
2278 st_softnet->flow_limit += st_softnet_i->flow_limit;
2284 /*------------------ END: FUNCTIONS USED BY SADC ONLY ---------------------*/
2285 #endif /* SOURCE_SADC */