2 * rd_stats.c: Read system statistics
3 * (C) 1999-2015 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>
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, "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);
271 else if (!strncmp(line, "AnonPages:", 10)) {
272 /* Read the amount of pages mapped into userspace page tables in kB */
273 sscanf(line + 10, "%lu", &st_memory->anonpgkb);
275 else if (!strncmp(line, "Slab:", 5)) {
276 /* Read the amount of in-kernel data structures cache in kB */
277 sscanf(line + 5, "%lu", &st_memory->slabkb);
279 else if (!strncmp(line, "KernelStack:", 12)) {
280 /* Read the kernel stack utilization in kB */
281 sscanf(line + 12, "%lu", &st_memory->kstackkb);
283 else if (!strncmp(line, "PageTables:", 11)) {
284 /* Read the amount of memory dedicated to the lowest level of page tables in kB */
285 sscanf(line + 11, "%lu", &st_memory->pgtblkb);
287 else if (!strncmp(line, "VmallocUsed:", 12)) {
288 /* Read the amount of vmalloc area which is used in kB */
289 sscanf(line + 12, "%lu", &st_memory->vmusedkb);
297 ***************************************************************************
298 * Read machine uptime, independently of the number of processors.
301 * @uptime Uptime value in jiffies.
302 ***************************************************************************
304 void read_uptime(unsigned long long *uptime)
308 unsigned long up_sec, up_cent;
310 if ((fp = fopen(UPTIME, "r")) == NULL)
313 if (fgets(line, sizeof(line), fp) == NULL) {
318 sscanf(line, "%lu.%lu", &up_sec, &up_cent);
319 *uptime = (unsigned long long) up_sec * HZ +
320 (unsigned long long) up_cent * HZ / 100;
327 /*---------------- BEGIN: FUNCTIONS USED BY SADC ONLY ---------------------*/
330 ***************************************************************************
331 * Replace octal codes in string with their corresponding characters.
334 * @str String to parse.
337 * @str String with octal codes replaced with characters.
338 ***************************************************************************
340 void oct2chr(char *str)
347 while (i < len - 3) {
348 if ((str[i] == '\\') &&
349 (str[i + 1] >= '0') && (str[i + 1] <= '3') &&
350 (str[i + 2] >= '0') && (str[i + 2] <= '7') &&
351 (str[i + 3] >= '0') && (str[i + 3] <= '7')) {
352 /* Octal code found */
353 str[i] = (str[i + 1] - 48) * 64 +
354 (str[i + 2] - 48) * 8 +
356 for (j = i + 4; j <= len; j++) {
366 ***************************************************************************
367 * Read processes (tasks) creation and context switches statistics
371 * @st_pcsw Structure where stats will be saved.
374 * @st_pcsw Structure with statistics.
375 ***************************************************************************
377 void read_stat_pcsw(struct stats_pcsw *st_pcsw)
382 if ((fp = fopen(STAT, "r")) == NULL)
385 while (fgets(line, sizeof(line), fp) != NULL) {
387 if (!strncmp(line, "ctxt ", 5)) {
388 /* Read number of context switches */
389 sscanf(line + 5, "%llu", &st_pcsw->context_switch);
392 else if (!strncmp(line, "processes ", 10)) {
393 /* Read number of processes created since system boot */
394 sscanf(line + 10, "%lu", &st_pcsw->processes);
402 ***************************************************************************
403 * Read queue and load statistics from /proc/loadavg and /proc/stat.
406 * @st_queue Structure where stats will be saved.
409 * @st_queue Structure with statistics.
410 ***************************************************************************
412 void read_loadavg(struct stats_queue *st_queue)
419 if ((fp = fopen(LOADAVG, "r")) == NULL)
422 /* Read load averages and queue length */
423 rc = fscanf(fp, "%d.%u %d.%u %d.%u %lu/%u %*d\n",
424 &load_tmp[0], &st_queue->load_avg_1,
425 &load_tmp[1], &st_queue->load_avg_5,
426 &load_tmp[2], &st_queue->load_avg_15,
427 &st_queue->nr_running,
428 &st_queue->nr_threads);
435 st_queue->load_avg_1 += load_tmp[0] * 100;
436 st_queue->load_avg_5 += load_tmp[1] * 100;
437 st_queue->load_avg_15 += load_tmp[2] * 100;
439 if (st_queue->nr_running) {
440 /* Do not take current process into account */
441 st_queue->nr_running--;
444 /* Read nr of tasks blocked from /proc/stat */
445 if ((fp = fopen(STAT, "r")) == NULL)
448 while (fgets(line, sizeof(line), fp) != NULL) {
450 if (!strncmp(line, "procs_blocked ", 14)) {
451 /* Read number of processes blocked */
452 sscanf(line + 14, "%lu", &st_queue->procs_blocked);
461 ***************************************************************************
462 * Read swapping statistics from /proc/vmstat.
465 * @st_swap Structure where stats will be saved.
468 * @st_swap Structure with statistics.
469 ***************************************************************************
471 void read_vmstat_swap(struct stats_swap *st_swap)
476 if ((fp = fopen(VMSTAT, "r")) == NULL)
479 while (fgets(line, sizeof(line), fp) != NULL) {
481 if (!strncmp(line, "pswpin ", 7)) {
482 /* Read number of swap pages brought in */
483 sscanf(line + 7, "%lu", &st_swap->pswpin);
485 else if (!strncmp(line, "pswpout ", 8)) {
486 /* Read number of swap pages brought out */
487 sscanf(line + 8, "%lu", &st_swap->pswpout);
495 ***************************************************************************
496 * Read paging statistics from /proc/vmstat.
499 * @st_paging Structure where stats will be saved.
502 * @st_paging Structure with statistics.
503 ***************************************************************************
505 void read_vmstat_paging(struct stats_paging *st_paging)
511 if ((fp = fopen(VMSTAT, "r")) == NULL)
514 st_paging->pgsteal = 0;
515 st_paging->pgscan_kswapd = st_paging->pgscan_direct = 0;
517 while (fgets(line, sizeof(line), fp) != NULL) {
519 if (!strncmp(line, "pgpgin ", 7)) {
520 /* Read number of pages the system paged in */
521 sscanf(line + 7, "%lu", &st_paging->pgpgin);
523 else if (!strncmp(line, "pgpgout ", 8)) {
524 /* Read number of pages the system paged out */
525 sscanf(line + 8, "%lu", &st_paging->pgpgout);
527 else if (!strncmp(line, "pgfault ", 8)) {
528 /* Read number of faults (major+minor) made by the system */
529 sscanf(line + 8, "%lu", &st_paging->pgfault);
531 else if (!strncmp(line, "pgmajfault ", 11)) {
532 /* Read number of faults (major only) made by the system */
533 sscanf(line + 11, "%lu", &st_paging->pgmajfault);
535 else if (!strncmp(line, "pgfree ", 7)) {
536 /* Read number of pages freed by the system */
537 sscanf(line + 7, "%lu", &st_paging->pgfree);
539 else if (!strncmp(line, "pgsteal_", 8)) {
540 /* Read number of pages stolen by the system */
541 sscanf(strchr(line, ' '), "%lu", &pgtmp);
542 st_paging->pgsteal += pgtmp;
544 else if (!strncmp(line, "pgscan_kswapd_", 14)) {
545 /* Read number of pages scanned by the kswapd daemon */
546 sscanf(strchr(line, ' '), "%lu", &pgtmp);
547 st_paging->pgscan_kswapd += pgtmp;
549 else if (!strncmp(line, "pgscan_direct_", 14)) {
550 /* Read number of pages scanned directly */
551 sscanf(strchr(line, ' '), "%lu", &pgtmp);
552 st_paging->pgscan_direct += pgtmp;
560 ***************************************************************************
561 * Read I/O and transfer rates statistics from /proc/diskstats.
564 * @st_io Structure where stats will be saved.
567 * @st_io Structure with statistics.
568 ***************************************************************************
570 void read_diskstats_io(struct stats_io *st_io)
574 char dev_name[MAX_NAME_LEN];
575 unsigned int major, minor;
576 unsigned long rd_ios, wr_ios, rd_sec, wr_sec;
578 if ((fp = fopen(DISKSTATS, "r")) == NULL)
581 while (fgets(line, sizeof(line), fp) != NULL) {
583 if (sscanf(line, "%u %u %s %lu %*u %lu %*u %lu %*u %lu",
584 &major, &minor, dev_name,
585 &rd_ios, &rd_sec, &wr_ios, &wr_sec) == 7) {
587 if (is_device(dev_name, IGNORE_VIRTUAL_DEVICES)) {
589 * OK: It's a (real) device and not a partition.
590 * Note: Structure should have been initialized first!
592 st_io->dk_drive += rd_ios + wr_ios;
593 st_io->dk_drive_rio += rd_ios;
594 st_io->dk_drive_rblk += rd_sec;
595 st_io->dk_drive_wio += wr_ios;
596 st_io->dk_drive_wblk += wr_sec;
605 ***************************************************************************
606 * Read block devices statistics from /proc/diskstats.
609 * @st_disk Structure where stats will be saved.
610 * @nbr Maximum number of block devices.
611 * @read_part True if disks *and* partitions should be read; False if only
615 * @st_disk Structure with statistics.
616 ***************************************************************************
618 void read_diskstats_disk(struct stats_disk *st_disk, int nbr, int read_part)
622 char dev_name[MAX_NAME_LEN];
624 struct stats_disk *st_disk_i;
625 unsigned int major, minor, rd_ticks, wr_ticks, tot_ticks, rq_ticks;
626 unsigned long rd_ios, wr_ios, rd_sec, wr_sec;
628 if ((fp = fopen(DISKSTATS, "r")) == NULL)
631 while ((fgets(line, sizeof(line), fp) != NULL) && (dsk < nbr)) {
633 if (sscanf(line, "%u %u %s %lu %*u %lu %u %lu %*u %lu"
635 &major, &minor, dev_name,
636 &rd_ios, &rd_sec, &rd_ticks, &wr_ios, &wr_sec, &wr_ticks,
637 &tot_ticks, &rq_ticks) == 11) {
639 if (!rd_ios && !wr_ios)
640 /* Unused device: Ignore it */
642 if (read_part || is_device(dev_name, ACCEPT_VIRTUAL_DEVICES)) {
643 st_disk_i = st_disk + dsk++;
644 st_disk_i->major = major;
645 st_disk_i->minor = minor;
646 st_disk_i->nr_ios = rd_ios + wr_ios;
647 st_disk_i->rd_sect = rd_sec;
648 st_disk_i->wr_sect = wr_sec;
649 st_disk_i->rd_ticks = rd_ticks;
650 st_disk_i->wr_ticks = wr_ticks;
651 st_disk_i->tot_ticks = tot_ticks;
652 st_disk_i->rq_ticks = rq_ticks;
661 ***************************************************************************
662 * Read serial lines statistics from /proc/tty/driver/serial.
665 * @st_serial Structure where stats will be saved.
666 * @nbr Maximum number of serial lines.
669 * @st_serial Structure with statistics.
670 ***************************************************************************
672 void read_tty_driver_serial(struct stats_serial *st_serial, int nbr)
675 struct stats_serial *st_serial_i;
680 if ((fp = fopen(SERIAL, "r")) == NULL)
683 while ((fgets(line, sizeof(line), fp) != NULL) && (sl < nbr)) {
685 if ((p = strstr(line, "tx:")) != NULL) {
686 st_serial_i = st_serial + sl;
687 sscanf(line, "%u", &st_serial_i->line);
689 * A value of 0 means an unused structure.
690 * So increment it to make sure it is not null.
692 (st_serial_i->line)++;
694 * Read the number of chars transmitted and received by
695 * current serial line.
697 sscanf(p + 3, "%u", &st_serial_i->tx);
698 if ((p = strstr(line, "rx:")) != NULL) {
699 sscanf(p + 3, "%u", &st_serial_i->rx);
701 if ((p = strstr(line, "fe:")) != NULL) {
702 sscanf(p + 3, "%u", &st_serial_i->frame);
704 if ((p = strstr(line, "pe:")) != NULL) {
705 sscanf(p + 3, "%u", &st_serial_i->parity);
707 if ((p = strstr(line, "brk:")) != NULL) {
708 sscanf(p + 4, "%u", &st_serial_i->brk);
710 if ((p = strstr(line, "oe:")) != NULL) {
711 sscanf(p + 3, "%u", &st_serial_i->overrun);
722 ***************************************************************************
723 * Read kernel tables statistics from various system files.
726 * @st_ktables Structure where stats will be saved.
729 * @st_ktables Structure with statistics.
730 ***************************************************************************
732 void read_kernel_tables(struct stats_ktables *st_ktables)
738 /* Open /proc/sys/fs/dentry-state file */
739 if ((fp = fopen(FDENTRY_STATE, "r")) != NULL) {
740 rc = fscanf(fp, "%*d %u",
741 &st_ktables->dentry_stat);
744 st_ktables->dentry_stat = 0;
748 /* Open /proc/sys/fs/file-nr file */
749 if ((fp = fopen(FFILE_NR, "r")) != NULL) {
750 rc = fscanf(fp, "%u %u",
751 &st_ktables->file_used, &parm);
754 * The number of used handles is the number of allocated ones
755 * minus the number of free ones.
758 st_ktables->file_used -= parm;
761 st_ktables->file_used = 0;
765 /* Open /proc/sys/fs/inode-state file */
766 if ((fp = fopen(FINODE_STATE, "r")) != NULL) {
767 rc = fscanf(fp, "%u %u",
768 &st_ktables->inode_used, &parm);
771 * The number of inuse inodes is the number of allocated ones
772 * minus the number of free ones.
775 st_ktables->inode_used -= parm;
778 st_ktables->inode_used = 0;
782 /* Open /proc/sys/kernel/pty/nr file */
783 if ((fp = fopen(PTY_NR, "r")) != NULL) {
784 rc = fscanf(fp, "%u",
785 &st_ktables->pty_nr);
788 st_ktables->pty_nr = 0;
794 ***************************************************************************
795 * Read network interfaces statistics from /proc/net/dev.
798 * @st_net_dev Structure where stats will be saved.
799 * @nbr Maximum number of network interfaces.
802 * @st_net_dev Structure with statistics.
805 * Number of interfaces for which stats have been read.
806 ***************************************************************************
808 int read_net_dev(struct stats_net_dev *st_net_dev, int nbr)
811 struct stats_net_dev *st_net_dev_i;
813 char iface[MAX_IFACE_LEN];
817 if ((fp = fopen(NET_DEV, "r")) == NULL)
820 while ((fgets(line, sizeof(line), fp) != NULL) && (dev < nbr)) {
822 pos = strcspn(line, ":");
823 if (pos < strlen(line)) {
824 st_net_dev_i = st_net_dev + dev;
825 strncpy(iface, line, MINIMUM(pos, MAX_IFACE_LEN - 1));
826 iface[MINIMUM(pos, MAX_IFACE_LEN - 1)] = '\0';
827 sscanf(iface, "%s", st_net_dev_i->interface); /* Skip heading spaces */
828 sscanf(line + pos + 1, "%llu %llu %*u %*u %*u %*u %llu %llu %llu %llu "
829 "%*u %*u %*u %*u %*u %llu",
830 &st_net_dev_i->rx_bytes,
831 &st_net_dev_i->rx_packets,
832 &st_net_dev_i->rx_compressed,
833 &st_net_dev_i->multicast,
834 &st_net_dev_i->tx_bytes,
835 &st_net_dev_i->tx_packets,
836 &st_net_dev_i->tx_compressed);
847 ***************************************************************************
848 * Read duplex and speed data for network interface cards.
851 * @st_net_dev Structure where stats will be saved.
852 * @nbr Real number of network interfaces available.
855 * @st_net_dev Structure with statistics.
856 ***************************************************************************
858 void read_if_info(struct stats_net_dev *st_net_dev, int nbr)
861 struct stats_net_dev *st_net_dev_i;
862 char filename[128], duplex[32];
865 for (dev = 0; dev < nbr; dev++) {
867 st_net_dev_i = st_net_dev + dev;
869 /* Read speed info */
870 sprintf(filename, IF_DUPLEX, st_net_dev_i->interface);
872 if ((fp = fopen(filename, "r")) == NULL)
873 /* Cannot read NIC duplex */
876 n = fscanf(fp, "%31s", duplex);
881 /* Cannot read NIC duplex */
884 if (!strcmp(duplex, K_DUPLEX_FULL)) {
885 st_net_dev_i->duplex = C_DUPLEX_FULL;
887 else if (!strcmp(duplex, K_DUPLEX_HALF)) {
888 st_net_dev_i->duplex = C_DUPLEX_HALF;
893 /* Read speed info */
894 sprintf(filename, IF_SPEED, st_net_dev_i->interface);
896 if ((fp = fopen(filename, "r")) == NULL)
897 /* Cannot read NIC speed */
900 n = fscanf(fp, "%u", &st_net_dev_i->speed);
905 st_net_dev_i->speed = 0;
912 ***************************************************************************
913 * Read network interfaces errors statistics from /proc/net/dev.
916 * @st_net_edev Structure where stats will be saved.
917 * @nbr Maximum number of network interfaces.
920 * @st_net_edev Structure with statistics.
921 ***************************************************************************
923 void read_net_edev(struct stats_net_edev *st_net_edev, int nbr)
926 struct stats_net_edev *st_net_edev_i;
927 static char line[256];
928 char iface[MAX_IFACE_LEN];
932 if ((fp = fopen(NET_DEV, "r")) == NULL)
935 while ((fgets(line, sizeof(line), fp) != NULL) && (dev < nbr)) {
937 pos = strcspn(line, ":");
938 if (pos < strlen(line)) {
939 st_net_edev_i = st_net_edev + dev;
940 strncpy(iface, line, MINIMUM(pos, MAX_IFACE_LEN - 1));
941 iface[MINIMUM(pos, MAX_IFACE_LEN - 1)] = '\0';
942 sscanf(iface, "%s", st_net_edev_i->interface); /* Skip heading spaces */
943 sscanf(line + pos + 1, "%*u %*u %llu %llu %llu %llu %*u %*u %*u %*u "
944 "%llu %llu %llu %llu %llu",
945 &st_net_edev_i->rx_errors,
946 &st_net_edev_i->rx_dropped,
947 &st_net_edev_i->rx_fifo_errors,
948 &st_net_edev_i->rx_frame_errors,
949 &st_net_edev_i->tx_errors,
950 &st_net_edev_i->tx_dropped,
951 &st_net_edev_i->tx_fifo_errors,
952 &st_net_edev_i->collisions,
953 &st_net_edev_i->tx_carrier_errors);
962 ***************************************************************************
963 * Read NFS client statistics from /proc/net/rpc/nfs.
966 * @st_net_nfs Structure where stats will be saved.
969 * @st_net_nfs Structure with statistics.
970 ***************************************************************************
972 void read_net_nfs(struct stats_net_nfs *st_net_nfs)
976 unsigned int getattcnt = 0, accesscnt = 0, readcnt = 0, writecnt = 0;
978 if ((fp = fopen(NET_RPC_NFS, "r")) == NULL)
981 memset(st_net_nfs, 0, STATS_NET_NFS_SIZE);
983 while (fgets(line, sizeof(line), fp) != NULL) {
985 if (!strncmp(line, "rpc ", 4)) {
986 sscanf(line + 4, "%u %u",
987 &st_net_nfs->nfs_rpccnt, &st_net_nfs->nfs_rpcretrans);
989 else if (!strncmp(line, "proc3 ", 6)) {
990 sscanf(line + 6, "%*u %*u %u %*u %*u %u %*u %u %u",
991 &getattcnt, &accesscnt, &readcnt, &writecnt);
993 st_net_nfs->nfs_getattcnt += getattcnt;
994 st_net_nfs->nfs_accesscnt += accesscnt;
995 st_net_nfs->nfs_readcnt += readcnt;
996 st_net_nfs->nfs_writecnt += writecnt;
998 else if (!strncmp(line, "proc4 ", 6)) {
999 sscanf(line + 6, "%*u %*u %u %u "
1000 "%*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %u %u",
1001 &readcnt, &writecnt, &accesscnt, &getattcnt);
1003 st_net_nfs->nfs_getattcnt += getattcnt;
1004 st_net_nfs->nfs_accesscnt += accesscnt;
1005 st_net_nfs->nfs_readcnt += readcnt;
1006 st_net_nfs->nfs_writecnt += writecnt;
1014 ***************************************************************************
1015 * Read NFS server statistics from /proc/net/rpc/nfsd.
1018 * @st_net_nfsd Structure where stats will be saved.
1021 * @st_net_nfsd Structure with statistics.
1022 ***************************************************************************
1024 void read_net_nfsd(struct stats_net_nfsd *st_net_nfsd)
1028 unsigned int getattcnt = 0, accesscnt = 0, readcnt = 0, writecnt = 0;
1030 if ((fp = fopen(NET_RPC_NFSD, "r")) == NULL)
1033 memset(st_net_nfsd, 0, STATS_NET_NFSD_SIZE);
1035 while (fgets(line, sizeof(line), fp) != NULL) {
1037 if (!strncmp(line, "rc ", 3)) {
1038 sscanf(line + 3, "%u %u",
1039 &st_net_nfsd->nfsd_rchits, &st_net_nfsd->nfsd_rcmisses);
1041 else if (!strncmp(line, "net ", 4)) {
1042 sscanf(line + 4, "%u %u %u",
1043 &st_net_nfsd->nfsd_netcnt, &st_net_nfsd->nfsd_netudpcnt,
1044 &st_net_nfsd->nfsd_nettcpcnt);
1046 else if (!strncmp(line, "rpc ", 4)) {
1047 sscanf(line + 4, "%u %u",
1048 &st_net_nfsd->nfsd_rpccnt, &st_net_nfsd->nfsd_rpcbad);
1050 else if (!strncmp(line, "proc3 ", 6)) {
1051 sscanf(line + 6, "%*u %*u %u %*u %*u %u %*u %u %u",
1052 &getattcnt, &accesscnt, &readcnt, &writecnt);
1054 st_net_nfsd->nfsd_getattcnt += getattcnt;
1055 st_net_nfsd->nfsd_accesscnt += accesscnt;
1056 st_net_nfsd->nfsd_readcnt += readcnt;
1057 st_net_nfsd->nfsd_writecnt += writecnt;
1060 else if (!strncmp(line, "proc4ops ", 9)) {
1061 sscanf(line + 9, "%*u %*u %*u %*u %u "
1062 "%*u %*u %*u %*u %*u %u "
1063 "%*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %u "
1064 "%*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %u",
1065 &accesscnt, &getattcnt, &readcnt, &writecnt);
1067 st_net_nfsd->nfsd_getattcnt += getattcnt;
1068 st_net_nfsd->nfsd_accesscnt += accesscnt;
1069 st_net_nfsd->nfsd_readcnt += readcnt;
1070 st_net_nfsd->nfsd_writecnt += writecnt;
1078 ***************************************************************************
1079 * Read network sockets statistics from /proc/net/sockstat.
1082 * @st_net_sock Structure where stats will be saved.
1085 * @st_net_sock Structure with statistics.
1086 ***************************************************************************
1088 void read_net_sock(struct stats_net_sock *st_net_sock)
1094 if ((fp = fopen(NET_SOCKSTAT, "r")) == NULL)
1097 while (fgets(line, sizeof(line), fp) != NULL) {
1099 if (!strncmp(line, "sockets:", 8)) {
1101 sscanf(line + 14, "%u", &st_net_sock->sock_inuse);
1103 else if (!strncmp(line, "TCP:", 4)) {
1105 sscanf(line + 11, "%u", &st_net_sock->tcp_inuse);
1106 if ((p = strstr(line, "tw")) != NULL) {
1107 sscanf(p + 2, "%u", &st_net_sock->tcp_tw);
1110 else if (!strncmp(line, "UDP:", 4)) {
1112 sscanf(line + 11, "%u", &st_net_sock->udp_inuse);
1114 else if (!strncmp(line, "RAW:", 4)) {
1116 sscanf(line + 11, "%u", &st_net_sock->raw_inuse);
1118 else if (!strncmp(line, "FRAG:", 5)) {
1120 sscanf(line + 12, "%u", &st_net_sock->frag_inuse);
1128 ***************************************************************************
1129 * Read IP network traffic statistics from /proc/net/snmp.
1132 * @st_net_ip Structure where stats will be saved.
1135 * @st_net_ip Structure with statistics.
1136 ***************************************************************************
1138 void read_net_ip(struct stats_net_ip *st_net_ip)
1144 if ((fp = fopen(NET_SNMP, "r")) == NULL)
1147 while (fgets(line, sizeof(line), fp) != NULL) {
1149 if (!strncmp(line, "Ip:", 3)) {
1151 sscanf(line + 3, "%*u %*u %llu %*u %*u %llu %*u %*u "
1152 "%llu %llu %*u %*u %*u %llu %llu %*u %llu %*u %llu",
1153 &st_net_ip->InReceives,
1154 &st_net_ip->ForwDatagrams,
1155 &st_net_ip->InDelivers,
1156 &st_net_ip->OutRequests,
1157 &st_net_ip->ReasmReqds,
1158 &st_net_ip->ReasmOKs,
1159 &st_net_ip->FragOKs,
1160 &st_net_ip->FragCreates);
1174 ***************************************************************************
1175 * Read IP network error statistics from /proc/net/snmp.
1178 * @st_net_eip Structure where stats will be saved.
1181 * @st_net_eip Structure with statistics.
1182 ***************************************************************************
1184 void read_net_eip(struct stats_net_eip *st_net_eip)
1190 if ((fp = fopen(NET_SNMP, "r")) == NULL)
1193 while (fgets(line, sizeof(line), fp) != NULL) {
1195 if (!strncmp(line, "Ip:", 3)) {
1197 sscanf(line + 3, "%*u %*u %*u %llu %llu %*u %llu %llu "
1198 "%*u %*u %llu %llu %*u %*u %*u %llu %*u %llu",
1199 &st_net_eip->InHdrErrors,
1200 &st_net_eip->InAddrErrors,
1201 &st_net_eip->InUnknownProtos,
1202 &st_net_eip->InDiscards,
1203 &st_net_eip->OutDiscards,
1204 &st_net_eip->OutNoRoutes,
1205 &st_net_eip->ReasmFails,
1206 &st_net_eip->FragFails);
1220 ***************************************************************************
1221 * Read ICMP network traffic statistics from /proc/net/snmp.
1224 * @st_net_icmp Structure where stats will be saved.
1227 * @st_net_icmp Structure with statistics.
1228 ***************************************************************************
1230 void read_net_icmp(struct stats_net_icmp *st_net_icmp)
1236 if ((fp = fopen(NET_SNMP, "r")) == NULL)
1239 while (fgets(line, sizeof(line), fp) != NULL) {
1241 if (!strncmp(line, "Icmp:", 5)) {
1243 sscanf(line + 5, "%lu %*u %*u %*u %*u %*u %*u "
1244 "%lu %lu %lu %lu %lu %lu %lu %*u %*u %*u %*u "
1245 "%*u %*u %lu %lu %lu %lu %lu %lu",
1246 &st_net_icmp->InMsgs,
1247 &st_net_icmp->InEchos,
1248 &st_net_icmp->InEchoReps,
1249 &st_net_icmp->InTimestamps,
1250 &st_net_icmp->InTimestampReps,
1251 &st_net_icmp->InAddrMasks,
1252 &st_net_icmp->InAddrMaskReps,
1253 &st_net_icmp->OutMsgs,
1254 &st_net_icmp->OutEchos,
1255 &st_net_icmp->OutEchoReps,
1256 &st_net_icmp->OutTimestamps,
1257 &st_net_icmp->OutTimestampReps,
1258 &st_net_icmp->OutAddrMasks,
1259 &st_net_icmp->OutAddrMaskReps);
1273 ***************************************************************************
1274 * Read ICMP network error statistics from /proc/net/snmp.
1277 * @st_net_eicmp Structure where stats will be saved.
1280 * @st_net_eicmp Structure with statistics.
1281 ***************************************************************************
1283 void read_net_eicmp(struct stats_net_eicmp *st_net_eicmp)
1289 if ((fp = fopen(NET_SNMP, "r")) == NULL)
1292 while (fgets(line, sizeof(line), fp) != NULL) {
1294 if (!strncmp(line, "Icmp:", 5)) {
1296 sscanf(line + 5, "%*u %lu %lu %lu %lu %lu %lu %*u %*u "
1297 "%*u %*u %*u %*u %*u %lu %lu %lu %lu %lu %lu",
1298 &st_net_eicmp->InErrors,
1299 &st_net_eicmp->InDestUnreachs,
1300 &st_net_eicmp->InTimeExcds,
1301 &st_net_eicmp->InParmProbs,
1302 &st_net_eicmp->InSrcQuenchs,
1303 &st_net_eicmp->InRedirects,
1304 &st_net_eicmp->OutErrors,
1305 &st_net_eicmp->OutDestUnreachs,
1306 &st_net_eicmp->OutTimeExcds,
1307 &st_net_eicmp->OutParmProbs,
1308 &st_net_eicmp->OutSrcQuenchs,
1309 &st_net_eicmp->OutRedirects);
1323 ***************************************************************************
1324 * Read TCP network traffic statistics from /proc/net/snmp.
1327 * @st_net_tcp Structure where stats will be saved.
1330 * @st_net_tcp Structure with statistics.
1331 ***************************************************************************
1333 void read_net_tcp(struct stats_net_tcp *st_net_tcp)
1339 if ((fp = fopen(NET_SNMP, "r")) == NULL)
1342 while (fgets(line, sizeof(line), fp) != NULL) {
1344 if (!strncmp(line, "Tcp:", 4)) {
1346 sscanf(line + 4, "%*u %*u %*u %*d %lu %lu "
1347 "%*u %*u %*u %lu %lu",
1348 &st_net_tcp->ActiveOpens,
1349 &st_net_tcp->PassiveOpens,
1350 &st_net_tcp->InSegs,
1351 &st_net_tcp->OutSegs);
1365 ***************************************************************************
1366 * Read TCP network error statistics from /proc/net/snmp.
1369 * @st_net_etcp Structure where stats will be saved.
1372 * @st_net_etcp Structure with statistics.
1373 ***************************************************************************
1375 void read_net_etcp(struct stats_net_etcp *st_net_etcp)
1381 if ((fp = fopen(NET_SNMP, "r")) == NULL)
1384 while (fgets(line, sizeof(line), fp) != NULL) {
1386 if (!strncmp(line, "Tcp:", 4)) {
1388 sscanf(line + 4, "%*u %*u %*u %*d %*u %*u "
1389 "%lu %lu %*u %*u %*u %lu %lu %lu",
1390 &st_net_etcp->AttemptFails,
1391 &st_net_etcp->EstabResets,
1392 &st_net_etcp->RetransSegs,
1393 &st_net_etcp->InErrs,
1394 &st_net_etcp->OutRsts);
1408 ***************************************************************************
1409 * Read UDP network traffic statistics from /proc/net/snmp.
1412 * @st_net_udp Structure where stats will be saved.
1415 * @st_net_udp Structure with statistics.
1416 ***************************************************************************
1418 void read_net_udp(struct stats_net_udp *st_net_udp)
1424 if ((fp = fopen(NET_SNMP, "r")) == NULL)
1427 while (fgets(line, sizeof(line), fp) != NULL) {
1429 if (!strncmp(line, "Udp:", 4)) {
1431 sscanf(line + 4, "%lu %lu %lu %lu",
1432 &st_net_udp->InDatagrams,
1433 &st_net_udp->NoPorts,
1434 &st_net_udp->InErrors,
1435 &st_net_udp->OutDatagrams);
1449 ***************************************************************************
1450 * Read IPv6 network sockets statistics from /proc/net/sockstat6.
1453 * @st_net_sock6 Structure where stats will be saved.
1456 * @st_net_sock6 Structure with statistics.
1457 ***************************************************************************
1459 void read_net_sock6(struct stats_net_sock6 *st_net_sock6)
1464 if ((fp = fopen(NET_SOCKSTAT6, "r")) == NULL)
1467 while (fgets(line, sizeof(line), fp) != NULL) {
1469 if (!strncmp(line, "TCP6:", 5)) {
1471 sscanf(line + 12, "%u", &st_net_sock6->tcp6_inuse);
1473 else if (!strncmp(line, "UDP6:", 5)) {
1475 sscanf(line + 12, "%u", &st_net_sock6->udp6_inuse);
1477 else if (!strncmp(line, "RAW6:", 5)) {
1478 /* IPv6 RAW sockets */
1479 sscanf(line + 12, "%u", &st_net_sock6->raw6_inuse);
1481 else if (!strncmp(line, "FRAG6:", 6)) {
1482 /* IPv6 FRAGments */
1483 sscanf(line + 13, "%u", &st_net_sock6->frag6_inuse);
1491 ***************************************************************************
1492 * Read IPv6 network traffic statistics from /proc/net/snmp6.
1495 * @st_net_ip6 Structure where stats will be saved.
1498 * @st_net_ip6 Structure with statistics.
1499 ***************************************************************************
1501 void read_net_ip6(struct stats_net_ip6 *st_net_ip6)
1506 if ((fp = fopen(NET_SNMP6, "r")) == NULL)
1509 while (fgets(line, sizeof(line), fp) != NULL) {
1511 if (!strncmp(line, "Ip6InReceives ", 14)) {
1512 sscanf(line + 14, "%llu", &st_net_ip6->InReceives6);
1514 else if (!strncmp(line, "Ip6OutForwDatagrams ", 20)) {
1515 sscanf(line + 20, "%llu", &st_net_ip6->OutForwDatagrams6);
1517 else if (!strncmp(line, "Ip6InDelivers ", 14)) {
1518 sscanf(line + 14, "%llu", &st_net_ip6->InDelivers6);
1520 else if (!strncmp(line, "Ip6OutRequests ", 15)) {
1521 sscanf(line + 15, "%llu", &st_net_ip6->OutRequests6);
1523 else if (!strncmp(line, "Ip6ReasmReqds ", 14)) {
1524 sscanf(line + 14, "%llu", &st_net_ip6->ReasmReqds6);
1526 else if (!strncmp(line, "Ip6ReasmOKs ", 12)) {
1527 sscanf(line + 12, "%llu", &st_net_ip6->ReasmOKs6);
1529 else if (!strncmp(line, "Ip6InMcastPkts ", 15)) {
1530 sscanf(line + 15, "%llu", &st_net_ip6->InMcastPkts6);
1532 else if (!strncmp(line, "Ip6OutMcastPkts ", 16)) {
1533 sscanf(line + 16, "%llu", &st_net_ip6->OutMcastPkts6);
1535 else if (!strncmp(line, "Ip6FragOKs ", 11)) {
1536 sscanf(line + 11, "%llu", &st_net_ip6->FragOKs6);
1538 else if (!strncmp(line, "Ip6FragCreates ", 15)) {
1539 sscanf(line + 15, "%llu", &st_net_ip6->FragCreates6);
1547 ***************************************************************************
1548 * Read IPv6 network error statistics from /proc/net/snmp6.
1551 * @st_net_eip6 Structure where stats will be saved.
1554 * @st_net_eip6 Structure with statistics.
1555 ***************************************************************************
1557 void read_net_eip6(struct stats_net_eip6 *st_net_eip6)
1562 if ((fp = fopen(NET_SNMP6, "r")) == NULL)
1565 while (fgets(line, sizeof(line), fp) != NULL) {
1567 if (!strncmp(line, "Ip6InHdrErrors ", 15)) {
1568 sscanf(line + 15, "%llu", &st_net_eip6->InHdrErrors6);
1570 else if (!strncmp(line, "Ip6InAddrErrors ", 16)) {
1571 sscanf(line + 16, "%llu", &st_net_eip6->InAddrErrors6);
1573 else if (!strncmp(line, "Ip6InUnknownProtos ", 19)) {
1574 sscanf(line + 19, "%llu", &st_net_eip6->InUnknownProtos6);
1576 else if (!strncmp(line, "Ip6InTooBigErrors ", 18)) {
1577 sscanf(line + 18, "%llu", &st_net_eip6->InTooBigErrors6);
1579 else if (!strncmp(line, "Ip6InDiscards ", 14)) {
1580 sscanf(line + 14, "%llu", &st_net_eip6->InDiscards6);
1582 else if (!strncmp(line, "Ip6OutDiscards ", 15)) {
1583 sscanf(line + 15, "%llu", &st_net_eip6->OutDiscards6);
1585 else if (!strncmp(line, "Ip6InNoRoutes ", 14)) {
1586 sscanf(line + 14, "%llu", &st_net_eip6->InNoRoutes6);
1588 else if (!strncmp(line, "Ip6OutNoRoutes ", 15)) {
1589 sscanf(line + 15, "%llu", &st_net_eip6->OutNoRoutes6);
1591 else if (!strncmp(line, "Ip6ReasmFails ", 14)) {
1592 sscanf(line + 14, "%llu", &st_net_eip6->ReasmFails6);
1594 else if (!strncmp(line, "Ip6FragFails ", 13)) {
1595 sscanf(line + 13, "%llu", &st_net_eip6->FragFails6);
1597 else if (!strncmp(line, "Ip6InTruncatedPkts ", 19)) {
1598 sscanf(line + 19, "%llu", &st_net_eip6->InTruncatedPkts6);
1606 ***************************************************************************
1607 * Read ICMPv6 network traffic statistics from /proc/net/snmp6.
1610 * @st_net_icmp6 Structure where stats will be saved.
1613 * @st_net_icmp6 Structure with statistics.
1614 ***************************************************************************
1616 void read_net_icmp6(struct stats_net_icmp6 *st_net_icmp6)
1621 if ((fp = fopen(NET_SNMP6, "r")) == NULL)
1624 while (fgets(line, sizeof(line), fp) != NULL) {
1626 if (!strncmp(line, "Icmp6InMsgs ", 12)) {
1627 sscanf(line + 12, "%lu", &st_net_icmp6->InMsgs6);
1629 else if (!strncmp(line, "Icmp6OutMsgs ", 13)) {
1630 sscanf(line + 13, "%lu", &st_net_icmp6->OutMsgs6);
1632 else if (!strncmp(line, "Icmp6InEchos ", 13)) {
1633 sscanf(line + 13, "%lu", &st_net_icmp6->InEchos6);
1635 else if (!strncmp(line, "Icmp6InEchoReplies ", 19)) {
1636 sscanf(line + 19, "%lu", &st_net_icmp6->InEchoReplies6);
1638 else if (!strncmp(line, "Icmp6OutEchoReplies ", 20)) {
1639 sscanf(line + 20, "%lu", &st_net_icmp6->OutEchoReplies6);
1641 else if (!strncmp(line, "Icmp6InGroupMembQueries ", 24)) {
1642 sscanf(line + 24, "%lu", &st_net_icmp6->InGroupMembQueries6);
1644 else if (!strncmp(line, "Icmp6InGroupMembResponses ", 26)) {
1645 sscanf(line + 26, "%lu", &st_net_icmp6->InGroupMembResponses6);
1647 else if (!strncmp(line, "Icmp6OutGroupMembResponses ", 27)) {
1648 sscanf(line + 27, "%lu", &st_net_icmp6->OutGroupMembResponses6);
1650 else if (!strncmp(line, "Icmp6InGroupMembReductions ", 27)) {
1651 sscanf(line + 27, "%lu", &st_net_icmp6->InGroupMembReductions6);
1653 else if (!strncmp(line, "Icmp6OutGroupMembReductions ", 28)) {
1654 sscanf(line + 28, "%lu", &st_net_icmp6->OutGroupMembReductions6);
1656 else if (!strncmp(line, "Icmp6InRouterSolicits ", 22)) {
1657 sscanf(line + 22, "%lu", &st_net_icmp6->InRouterSolicits6);
1659 else if (!strncmp(line, "Icmp6OutRouterSolicits ", 23)) {
1660 sscanf(line + 23, "%lu", &st_net_icmp6->OutRouterSolicits6);
1662 else if (!strncmp(line, "Icmp6InRouterAdvertisements ", 28)) {
1663 sscanf(line + 28, "%lu", &st_net_icmp6->InRouterAdvertisements6);
1665 else if (!strncmp(line, "Icmp6InNeighborSolicits ", 24)) {
1666 sscanf(line + 24, "%lu", &st_net_icmp6->InNeighborSolicits6);
1668 else if (!strncmp(line, "Icmp6OutNeighborSolicits ", 25)) {
1669 sscanf(line + 25, "%lu", &st_net_icmp6->OutNeighborSolicits6);
1671 else if (!strncmp(line, "Icmp6InNeighborAdvertisements ", 30)) {
1672 sscanf(line + 30, "%lu", &st_net_icmp6->InNeighborAdvertisements6);
1674 else if (!strncmp(line, "Icmp6OutNeighborAdvertisements ", 31)) {
1675 sscanf(line + 31, "%lu", &st_net_icmp6->OutNeighborAdvertisements6);
1683 ***************************************************************************
1684 * Read ICMPv6 network error statistics from /proc/net/snmp6.
1687 * @st_net_eicmp6 Structure where stats will be saved.
1690 * @st_net_eicmp6 Structure with statistics.
1691 ***************************************************************************
1693 void read_net_eicmp6(struct stats_net_eicmp6 *st_net_eicmp6)
1698 if ((fp = fopen(NET_SNMP6, "r")) == NULL)
1701 while (fgets(line, sizeof(line), fp) != NULL) {
1703 if (!strncmp(line, "Icmp6InErrors ", 14)) {
1704 sscanf(line + 14, "%lu", &st_net_eicmp6->InErrors6);
1706 else if (!strncmp(line, "Icmp6InDestUnreachs ", 20)) {
1707 sscanf(line + 20, "%lu", &st_net_eicmp6->InDestUnreachs6);
1709 else if (!strncmp(line, "Icmp6OutDestUnreachs ", 21)) {
1710 sscanf(line + 21, "%lu", &st_net_eicmp6->OutDestUnreachs6);
1712 else if (!strncmp(line, "Icmp6InTimeExcds ", 17)) {
1713 sscanf(line + 17, "%lu", &st_net_eicmp6->InTimeExcds6);
1715 else if (!strncmp(line, "Icmp6OutTimeExcds ", 18)) {
1716 sscanf(line + 18, "%lu", &st_net_eicmp6->OutTimeExcds6);
1718 else if (!strncmp(line, "Icmp6InParmProblems ", 20)) {
1719 sscanf(line + 20, "%lu", &st_net_eicmp6->InParmProblems6);
1721 else if (!strncmp(line, "Icmp6OutParmProblems ", 21)) {
1722 sscanf(line + 21, "%lu", &st_net_eicmp6->OutParmProblems6);
1724 else if (!strncmp(line, "Icmp6InRedirects ", 17)) {
1725 sscanf(line + 17, "%lu", &st_net_eicmp6->InRedirects6);
1727 else if (!strncmp(line, "Icmp6OutRedirects ", 18)) {
1728 sscanf(line + 18, "%lu", &st_net_eicmp6->OutRedirects6);
1730 else if (!strncmp(line, "Icmp6InPktTooBigs ", 18)) {
1731 sscanf(line + 18, "%lu", &st_net_eicmp6->InPktTooBigs6);
1733 else if (!strncmp(line, "Icmp6OutPktTooBigs ", 19)) {
1734 sscanf(line + 19, "%lu", &st_net_eicmp6->OutPktTooBigs6);
1742 ***************************************************************************
1743 * Read UDPv6 network traffic statistics from /proc/net/snmp6.
1746 * @st_net_udp6 Structure where stats will be saved.
1749 * @st_net_udp6 Structure with statistics.
1750 ***************************************************************************
1752 void read_net_udp6(struct stats_net_udp6 *st_net_udp6)
1757 if ((fp = fopen(NET_SNMP6, "r")) == NULL)
1760 while (fgets(line, sizeof(line), fp) != NULL) {
1762 if (!strncmp(line, "Udp6InDatagrams ", 16)) {
1763 sscanf(line + 16, "%lu", &st_net_udp6->InDatagrams6);
1765 else if (!strncmp(line, "Udp6OutDatagrams ", 17)) {
1766 sscanf(line + 17, "%lu", &st_net_udp6->OutDatagrams6);
1768 else if (!strncmp(line, "Udp6NoPorts ", 12)) {
1769 sscanf(line + 12, "%lu", &st_net_udp6->NoPorts6);
1771 else if (!strncmp(line, "Udp6InErrors ", 13)) {
1772 sscanf(line + 13, "%lu", &st_net_udp6->InErrors6);
1780 ***************************************************************************
1781 * Read CPU frequency statistics.
1784 * @st_pwr_cpufreq Structure where stats will be saved.
1785 * @nbr Total number of CPU (including cpu "all").
1788 * @st_pwr_cpufreq Structure with statistics.
1789 ***************************************************************************
1791 void read_cpuinfo(struct stats_pwr_cpufreq *st_pwr_cpufreq, int nbr)
1794 struct stats_pwr_cpufreq *st_pwr_cpufreq_i;
1797 unsigned int proc_nb = 0, ifreq, dfreq;
1799 if ((fp = fopen(CPUINFO, "r")) == NULL)
1802 st_pwr_cpufreq->cpufreq = 0;
1804 while (fgets(line, sizeof(line), fp) != NULL) {
1806 if (!strncmp(line, "processor\t", 10)) {
1807 sscanf(strchr(line, ':') + 1, "%u", &proc_nb);
1810 else if (!strncmp(line, "cpu MHz\t", 8)) {
1811 sscanf(strchr(line, ':') + 1, "%u.%u", &ifreq, &dfreq);
1813 if (proc_nb < (nbr - 1)) {
1814 /* Save current CPU frequency */
1815 st_pwr_cpufreq_i = st_pwr_cpufreq + proc_nb + 1;
1816 st_pwr_cpufreq_i->cpufreq = ifreq * 100 + dfreq / 10;
1818 /* Also save it to compute an average CPU frequency */
1819 st_pwr_cpufreq->cpufreq += st_pwr_cpufreq_i->cpufreq;
1822 else if (!proc_nb && (nbr == 1)) {
1824 * We are reading freq for "Processor 0" and we have a machine
1825 * with only one processor and not an SMP kernel, with /sys not mounted
1826 * (the nr of proc has been counted using /proc/stat and there was
1827 * only one line with global CPU stats here).
1828 * This is a very specific case, I must admit...
1830 st_pwr_cpufreq->cpufreq = ifreq * 100 + dfreq / 10;
1838 /* Compute average CPU frequency for this machine */
1839 st_pwr_cpufreq->cpufreq /= nr;
1844 ***************************************************************************
1845 * Read hugepages statistics from /proc/meminfo.
1848 * @st_huge Structure where stats will be saved.
1851 * @st_huge Structure with statistics.
1852 ***************************************************************************
1854 void read_meminfo_huge(struct stats_huge *st_huge)
1858 unsigned long szhkb = 0;
1860 if ((fp = fopen(MEMINFO, "r")) == NULL)
1863 while (fgets(line, sizeof(line), fp) != NULL) {
1865 if (!strncmp(line, "HugePages_Total:", 16)) {
1866 /* Read the total number of huge pages */
1867 sscanf(line + 16, "%lu", &st_huge->tlhkb);
1869 else if (!strncmp(line, "HugePages_Free:", 15)) {
1870 /* Read the number of free huge pages */
1871 sscanf(line + 15, "%lu", &st_huge->frhkb);
1873 else if (!strncmp(line, "Hugepagesize:", 13)) {
1874 /* Read the default size of a huge page in kB */
1875 sscanf(line + 13, "%lu", &szhkb);
1881 /* We want huge pages stats in kB and not expressed in a number of pages */
1882 st_huge->tlhkb *= szhkb;
1883 st_huge->frhkb *= szhkb;
1887 ***************************************************************************
1888 * Read CPU average frequencies statistics.
1891 * @st_pwr_wghfreq Structure where stats will be saved.
1892 * @cpu_nr CPU number for which time_in_state date will be read.
1893 * @nbr Total number of states (frequencies).
1896 * @st_pwr_wghfreq Structure with statistics.
1897 ***************************************************************************
1899 void read_time_in_state(struct stats_pwr_wghfreq *st_pwr_wghfreq, int cpu_nr, int nbr)
1902 struct stats_pwr_wghfreq *st_pwr_wghfreq_j;
1903 char filename[MAX_PF_NAME];
1907 unsigned long long time_in_state;
1909 snprintf(filename, MAX_PF_NAME, "%s/cpu%d/%s",
1910 SYSFS_DEVCPU, cpu_nr, SYSFS_TIME_IN_STATE);
1911 if ((fp = fopen(filename, "r")) == NULL)
1914 while (fgets(line, sizeof(line), fp) != NULL) {
1916 sscanf(line, "%lu %llu", &freq, &time_in_state);
1919 /* Save current frequency and time */
1920 st_pwr_wghfreq_j = st_pwr_wghfreq + j;
1921 st_pwr_wghfreq_j->freq = freq;
1922 st_pwr_wghfreq_j->time_in_state = time_in_state;
1931 ***************************************************************************
1932 * Read current USB device data.
1935 * @st_pwr_usb Structure where stats will be saved.
1936 * @usb_device File name for current USB device.
1939 * @st_pwr_usb Structure with statistics.
1940 ***************************************************************************
1942 void read_usb_stats(struct stats_pwr_usb *st_pwr_usb, char *usb_device)
1947 char filename[MAX_PF_NAME];
1949 /* Get USB device bus number */
1950 sscanf(usb_device, "%u", &st_pwr_usb->bus_nr);
1952 /* Read USB device vendor ID */
1953 snprintf(filename, MAX_PF_NAME, "%s/%s/%s",
1954 SYSFS_USBDEV, usb_device, SYSFS_IDVENDOR);
1955 if ((fp = fopen(filename, "r")) != NULL) {
1956 rc = fscanf(fp, "%x",
1957 &st_pwr_usb->vendor_id);
1960 st_pwr_usb->vendor_id = 0;
1964 /* Read USB device product ID */
1965 snprintf(filename, MAX_PF_NAME, "%s/%s/%s",
1966 SYSFS_USBDEV, usb_device, SYSFS_IDPRODUCT);
1967 if ((fp = fopen(filename, "r")) != NULL) {
1968 rc = fscanf(fp, "%x",
1969 &st_pwr_usb->product_id);
1972 st_pwr_usb->product_id = 0;
1976 /* Read USB device max power consumption */
1977 snprintf(filename, MAX_PF_NAME, "%s/%s/%s",
1978 SYSFS_USBDEV, usb_device, SYSFS_BMAXPOWER);
1979 if ((fp = fopen(filename, "r")) != NULL) {
1980 rc = fscanf(fp, "%u",
1981 &st_pwr_usb->bmaxpower);
1984 st_pwr_usb->bmaxpower = 0;
1988 /* Read USB device manufacturer */
1989 snprintf(filename, MAX_PF_NAME, "%s/%s/%s",
1990 SYSFS_USBDEV, usb_device, SYSFS_MANUFACTURER);
1991 if ((fp = fopen(filename, "r")) != NULL) {
1992 rs = fgets(st_pwr_usb->manufacturer,
1993 MAX_MANUF_LEN - 1, fp);
1996 (l = strlen(st_pwr_usb->manufacturer)) > 0) {
1997 /* Remove trailing CR */
1998 st_pwr_usb->manufacturer[l - 1] = '\0';
2002 /* Read USB device product */
2003 snprintf(filename, MAX_PF_NAME, "%s/%s/%s",
2004 SYSFS_USBDEV, usb_device, SYSFS_PRODUCT);
2005 if ((fp = fopen(filename, "r")) != NULL) {
2006 rs = fgets(st_pwr_usb->product,
2007 MAX_PROD_LEN - 1, fp);
2010 (l = strlen(st_pwr_usb->product)) > 0) {
2011 /* Remove trailing CR */
2012 st_pwr_usb->product[l - 1] = '\0';
2018 ***************************************************************************
2019 * Read USB devices statistics.
2022 * @st_pwr_usb Structure where stats will be saved.
2023 * @nbr Total number of USB devices.
2026 * @st_pwr_usb Structure with statistics.
2027 ***************************************************************************
2029 void read_bus_usb_dev(struct stats_pwr_usb *st_pwr_usb, int nbr)
2033 struct stats_pwr_usb *st_pwr_usb_j;
2036 /* Open relevant /sys directory */
2037 if ((dir = opendir(SYSFS_USBDEV)) == NULL)
2040 /* Get current file entry */
2041 while ((drd = readdir(dir)) != NULL) {
2043 if (isdigit(drd->d_name[0]) && !strchr(drd->d_name, ':')) {
2045 /* Read current USB device data */
2046 st_pwr_usb_j = st_pwr_usb + j;
2047 read_usb_stats(st_pwr_usb_j, drd->d_name);
2055 /* Close directory */
2060 ***************************************************************************
2061 * Read filesystems statistics.
2064 * @st_filesystem Structure where stats will be saved.
2065 * @nbr Total number of filesystems.
2068 * @st_filesystem Structure with statistics.
2069 ***************************************************************************
2071 void read_filesystem(struct stats_filesystem *st_filesystem, int nbr)
2074 char line[512], fs_name[MAX_FS_LEN], mountp[256];
2076 struct stats_filesystem *st_filesystem_i;
2079 if ((fp = fopen(MTAB, "r")) == NULL)
2082 while ((fgets(line, sizeof(line), fp) != NULL) && (fs < nbr)) {
2083 if (line[0] == '/') {
2085 /* Read current filesystem name */
2086 sscanf(line, "%127s", fs_name);
2088 * And now read the corresponding mount point.
2089 * Read fs name and mount point in two distinct operations.
2090 * Indeed, if fs name length is greater than 127 chars,
2091 * previous scanf() will read only the first 127 chars, and
2092 * mount point name will be read using the remaining chars
2093 * from the fs name. This will result in a bogus name
2094 * and following statvfs() function will always fail.
2096 sscanf(strchr(line, ' ') + 1, "%255s", mountp);
2098 /* Replace octal codes */
2102 * It's important to have read the whole mount point name
2103 * for statvfs() to work properly (see above).
2105 if ((statvfs(mountp, &buf) < 0) || (!buf.f_blocks))
2108 st_filesystem_i = st_filesystem + fs++;
2109 st_filesystem_i->f_blocks = buf.f_blocks * buf.f_frsize;
2110 st_filesystem_i->f_bfree = buf.f_bfree * buf.f_frsize;
2111 st_filesystem_i->f_bavail = buf.f_bavail * buf.f_frsize;
2112 st_filesystem_i->f_files = buf.f_files;
2113 st_filesystem_i->f_ffree = buf.f_ffree;
2114 strcpy(st_filesystem_i->fs_name, fs_name);
2115 strncpy(st_filesystem_i->mountp, mountp, MAX_FS_LEN);
2116 st_filesystem_i->mountp[MAX_FS_LEN - 1] = '\0';
2124 ***************************************************************************
2125 * Read Fibre Channel HBA statistics.
2128 * @st_fc Structure where stats will be saved.
2129 * @nbr Total number of HBAs.
2132 * @st_fc Structure with statistics.
2133 ***************************************************************************
2135 void read_fchost(struct stats_fchost *st_fc, int nbr)
2140 struct stats_fchost *st_fc_i;
2142 char fcstat_filename[MAX_FS_LEN];
2144 unsigned long rx_frames, tx_frames, rx_words, tx_words;
2146 /* Each host, if present, will have its own hostX entry within SYSFS_FCHOST */
2147 if ((dir = opendir(SYSFS_FCHOST)) == NULL)
2148 return; /* No FC hosts */
2151 * Read each of the counters via sysfs, where they are
2152 * returned as hex values (e.g. 0x72400).
2154 while ((drd = readdir(dir)) != NULL) {
2155 rx_frames = tx_frames = rx_words = tx_words = 0;
2157 if (strncmp(drd->d_name, "host",4) == 0) {
2158 sprintf(fcstat_filename, "%s/%s/statistics/rx_frames",
2159 SYSFS_FCHOST, drd->d_name);
2160 if ((fp = fopen(fcstat_filename, "r"))) {
2161 if (fgets(line, sizeof(line), fp))
2162 sscanf(line, "%lx", &rx_frames);
2166 sprintf(fcstat_filename, "%s/%s/statistics/tx_frames",
2167 SYSFS_FCHOST, drd->d_name);
2168 if ((fp = fopen(fcstat_filename, "r"))) {
2169 if (fgets(line, sizeof(line), fp))
2170 sscanf(line, "%lx", &tx_frames);
2174 sprintf(fcstat_filename, "%s/%s/statistics/rx_words",
2175 SYSFS_FCHOST,drd->d_name);
2176 if ((fp = fopen(fcstat_filename, "r"))) {
2177 if (fgets(line, sizeof(line), fp))
2178 sscanf(line, "%lx", &rx_words);
2182 sprintf(fcstat_filename, "%s/%s/statistics/tx_words",
2183 SYSFS_FCHOST, drd->d_name);
2184 if ((fp = fopen(fcstat_filename, "r"))) {
2185 if (fgets(line, sizeof(line), fp))
2186 sscanf(line, "%lx", &tx_words);
2190 st_fc_i = st_fc + fch++;
2191 st_fc_i->f_rxframes = rx_frames;
2192 st_fc_i->f_txframes = tx_frames;
2193 st_fc_i->f_rxwords = rx_words;
2194 st_fc_i->f_txwords = tx_words;
2195 strcpy(st_fc_i->fchost_name, drd->d_name);
2202 /*------------------ END: FUNCTIONS USED BY SADC ONLY ---------------------*/
2203 #endif /* SOURCE_SADC */