-
Notifications
You must be signed in to change notification settings - Fork 23
/
uptimed.c
153 lines (126 loc) · 3.9 KB
/
uptimed.c
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <sys/sysinfo.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include "statsd-client.h"
static int running = 1;
void sigterm(int sig)
{
running = 0;
}
#define MAX_LINE_LEN 200
#define PKT_LEN 1400
int find_my_mac(const char *iface, char *mac)
{
struct ifreq ifr;
memset(&ifr, 0, sizeof(struct ifreq));
strncpy(ifr.ifr_name, iface, IFNAMSIZ);
int s = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
if (s == -1) {
perror("socket");
return -1;
}
if ( ioctl(s, SIOCGIFHWADDR, &ifr) == -1 ) {
perror("ioctl(SIOCGIFHWADDR)");
close(s);
return -1;
}
unsigned char macaddr[6];
memcpy(macaddr, ifr.ifr_hwaddr.sa_data, 6);
sprintf(mac, "%.2X%.2X%.2X%.2X%.2X%.2X",
macaddr[0], macaddr[1], macaddr[2],
macaddr[3], macaddr[4], macaddr[5]);
close(s);
return 0;
}
int main(int argc, char *argv[])
{
FILE *net, *stat;
struct sysinfo si;
char line[MAX_LINE_LEN], tmp[MAX_LINE_LEN], pkt[PKT_LEN], ns[16];
char *lasts, *iface, *p;
unsigned int user, nice, sys, idle, total, busy, old_total=0, old_busy=0;
statsd_link *link;
int i;
if (argc != 3) {
printf("Usage: %s host interface\n", argv[0]);
exit(1);
}
iface = argv[2];
strcpy(ns, "host.");
if (find_my_mac(iface, ns + 5)!=0) {
fprintf(stderr, "Cannot find mac address\n");
}
signal(SIGHUP, SIG_IGN);
signal(SIGPIPE, SIG_IGN);
signal(SIGCHLD, SIG_IGN); /* will save one syscall per sleep */
signal(SIGTERM, sigterm);
if ( (net = fopen("/proc/net/dev", "r")) == NULL) {
perror("fopen");
exit(-1);
}
if ( (stat = fopen("/proc/stat", "r")) == NULL) {
perror("fopen");
exit(-1);
}
link = statsd_init_with_namespace(argv[1], 8125, ns);
daemon(0,0);
while(running) {
pkt[0] = 0;
sysinfo(&si);
statsd_prepare(link, "load", 100*si.loads[0]/0x10000, "g", 1.0, tmp, MAX_LINE_LEN, 1);
strcat(pkt, tmp);
statsd_prepare(link, "freemem", si.freeram/1024, "g", 1.0, tmp, MAX_LINE_LEN, 1);
strcat(pkt, tmp);
/*
statsd_prepare("freeswap", si.freeswap/1024, "g", 1.0, tmp, MAX_LINE_LEN, 1);
strcat(pkt, tmp);
*/
statsd_prepare(link, "procs", si.procs, "g", 1.0, tmp, MAX_LINE_LEN, 1);
strcat(pkt, tmp);
statsd_prepare(link, "uptime", si.uptime, "c", 1.0, tmp, MAX_LINE_LEN, 1);
strcat(pkt, tmp);
rewind(net);
while(!feof(net)) {
fgets(line, MAX_LINE_LEN, net);
if (!strstr(line, iface)) {
continue;
}
i = 0;
for (i = 0, p = strtok_r(line, " ", &lasts); p;
p = strtok_r(NULL, " ", &lasts), i++) {
if (i == 1) {
statsd_prepare(link, "if-rx", atoi(p), "c", 1.0, tmp, MAX_LINE_LEN, 1);
strcat(pkt, tmp);
} else if (i == 9) {
statsd_prepare(link, "if-tx", atoi(p), "c", 1.0, tmp, MAX_LINE_LEN, 1);
strcat(pkt, tmp);
break;
}
}
}
/* rewind doesn't do the trick for /proc/stat */
freopen("/proc/stat", "r", stat);
fgets(line, MAX_LINE_LEN, stat);
sscanf(line, "cpu %u %u %u %u", &user, &nice, &sys, &idle);
total = user + sys + idle;
busy = user + sys;
statsd_prepare(link, "cpu", 100 * (busy - old_busy)/(total - old_total), "g", 1.0, tmp, MAX_LINE_LEN, 0);
strcat(pkt, tmp);
// printf("pkt:\n%s\n\n", pkt);
statsd_send(link, pkt);
old_total = total;
old_busy = busy;
sleep(60);
}
fclose(net);
fclose(stat);
statsd_finalize(link);
exit(0);
}