File: getdata26.c

package info (click to toggle)
rstatd 4.0.1-12
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 672 kB
  • sloc: ansic: 3,895; makefile: 733; sh: 697
file content (92 lines) | stat: -rw-r--r-- 2,236 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
 * getdata26.c -- implementations of kernel 2.4 specific data retrieval functions
 *
 * software distributed under the GPL, for license terms see the file
 * COPYING in this directory
 *
 * (c) 2005 Dr. Andreas Mueller, Beratung und Entwicklung
 * $Id: getdata26.c,v 1.2 2005/09/19 07:20:33 afm Exp $
 */
#include "getdata.h"
#include "getdata26.h"
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>

/*
The 2.6 kernel moves disk statistics information from /proc/partitions
to /proc/diskstat, and removes the two line header
*/

void	get_disk26(struct statsusers *s) {
	FILE	*diskstat;
	unsigned long long	rio;
	unsigned long long	wio;
	char	line[1024];
	char	device[128];
	int	diskno;

	/* open the /proc/diskstat file */
	diskstat = fopen("/proc/diskstats", "r");
	if (NULL == diskstat) {
		return;
	}

	/* accumulate all the io statistics */
	diskno = 0;
	while ((diskno < 4) && (line == fgets(line, sizeof(line), diskstat))) {
		sscanf(line, " %*d %*d %s "
			"%lld %*d %*d %*d "
			"%lld %*d %*d %*d "
			"%*d %*d %*d\n",
			device, &rio, &wio);
		if (isdisk(device)) {
			s->dk_xfer[diskno++] = rio + wio;
		}
	}

	/* close the file */
	fclose(diskstat);
}

void	get_vm26(struct statsusers *s) {
	char	buf[1024];
	FILE	*vmstat;

	/* open the /proc/vmstat file */
	vmstat = fopen("/proc/vmstat", "r");
	if (NULL == vmstat) {
		return;
	}

	/* read the data from vmstat into the statsusers structure */
	while (buf == fgets(buf, sizeof(buf), vmstat)) {
		if (0 == strncmp(buf, "pgpgin", 6)) {
			if (1 != sscanf(buf, "pgpgin %u", &s->v_pgpgin)) {
				syslog(LOG_CRIT, "bad format in /proc/vmstat");
				exit(EXIT_FAILURE);
			}
		}
		if (0 == strncmp(buf, "pgpgout", 7)) {
			if (1 != sscanf(buf, "pgpgout %u", &s->v_pgpgout)) {
				syslog(LOG_CRIT, "bad format in /proc/vmstat");
				exit(EXIT_FAILURE);
			}
		}
		if (0 == strncmp(buf, "pswpin", 6)) {
			if (1 != sscanf(buf, "pswpin %u", &s->v_pswpin)) {
				syslog(LOG_CRIT, "bad format in /proc/vmstat");
				exit(EXIT_FAILURE);
			}
		}
		if (0 == strncmp(buf, "pswpout", 7)) {
			if (1 != sscanf(buf, "pswpout %u", &s->v_pswpout)) {
				syslog(LOG_CRIT, "bad format in /proc/vmstat");
				exit(EXIT_FAILURE);
			}
		}
	}

	/* close the file */
	fclose(vmstat);
}