Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b131546

Browse files
committed
complete code
1 parent a42cdc2 commit b131546

File tree

8 files changed

+454
-3
lines changed

8 files changed

+454
-3
lines changed

Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
all:
3+
make -C src/;
4+
make -C demo/;
5+
@for f in demo/*; do test -x $$f && cp -v $$f ./; done; echo
6+
7+
clean:
8+
make -C src/ clean;
9+
make -C demo/ clean;
10+

README.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
1-
statsd-client-cpp
2-
=================
1+
# a client sdk to feed data into StatsD, written in C++
2+
See [header file](src/statsd_client.h) for more api detail.
33

4-
a cpp client sdk to feed data into statsd
4+
5+
Included is a daemon for monitoring a Linux system.
6+
It'll wake up every minute and monitor the following:
7+
8+
* load
9+
* cpu
10+
* free memory
11+
* free swap (disabled)
12+
* received bytes
13+
* transmitted bytes
14+
* procs
15+
* uptime
16+
17+
The stats sent to statsd will be in "host.MACAddress" namespace.
18+
19+
Usage:
20+
21+
uptimed statsd-host interface-to-monitor
22+
23+
e.g.
24+
25+
uptimed 172.16.42.1 eth0

demo/Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
CXX=g++
3+
CPPFLAGS += -Wall -g
4+
5+
STATSD_INC = -I ../src/
6+
STATSD_LIB = -L ../src/ -lstatsd_client
7+
TARGETS=test_client system_monitor
8+
9+
all: $(TARGETS)
10+
11+
test_client: test_client.cpp
12+
$(CXX) -o $@ $< $(CPPFLAGS) $(STATSD_INC) $(STATSD_LIB)
13+
14+
system_monitor: system_monitor.cpp
15+
$(CXX) -o $@ $< $(CPPFLAGS) $(STATSD_INC) $(STATSD_LIB)
16+
17+
clean:
18+
rm -f $(TARGETS)
19+

demo/system_monitor.cpp

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#include <sys/types.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <signal.h>
5+
#include <unistd.h>
6+
#include <string.h>
7+
#include <sys/sysinfo.h>
8+
9+
#include <sys/ioctl.h>
10+
#include <netinet/in.h>
11+
#include <net/if.h>
12+
13+
#include "statsd_client.h"
14+
using namespace statsd;
15+
16+
static int running = 1;
17+
18+
void sigterm(int sig)
19+
{
20+
running = 0;
21+
}
22+
23+
#define MAX_LINE_LEN 200
24+
#define PKT_LEN 1400
25+
26+
int find_my_mac(const char *iface, char *mac)
27+
{
28+
struct ifreq ifr;
29+
memset(&ifr, 0, sizeof(struct ifreq));
30+
strncpy(ifr.ifr_name, iface, IFNAMSIZ);
31+
int s = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
32+
if (s == -1) {
33+
perror("socket");
34+
return -1;
35+
}
36+
37+
if ( ioctl(s, SIOCGIFHWADDR, &ifr) == -1 ) {
38+
perror("ioctl(SIOCGIFHWADDR)");
39+
close(s);
40+
return -1;
41+
}
42+
unsigned char macaddr[6];
43+
memcpy(macaddr, ifr.ifr_hwaddr.sa_data, 6);
44+
sprintf(mac, "%.2X%.2X%.2X%.2X%.2X%.2X",
45+
macaddr[0], macaddr[1], macaddr[2],
46+
macaddr[3], macaddr[4], macaddr[5]);
47+
close(s);
48+
return 0;
49+
}
50+
51+
int main(int argc, char *argv[])
52+
{
53+
FILE *net, *stat;
54+
struct sysinfo si;
55+
char line[MAX_LINE_LEN], ns[16];
56+
char *lasts, *iface, *p;
57+
unsigned int user, nice, sys, idle, total, busy, old_total=0, old_busy=0;
58+
int i;
59+
60+
if (argc != 3) {
61+
printf("Usage: %s host interface\n\nEg: %s 127.0.0.1 eth0", argv[0], argv[0]);
62+
exit(1);
63+
}
64+
iface = argv[2];
65+
strcpy(ns, "host.");
66+
if (find_my_mac(iface, ns + 5)!=0) {
67+
fprintf(stderr, "Cannot find mac address\n");
68+
}
69+
70+
signal(SIGHUP, SIG_IGN);
71+
signal(SIGPIPE, SIG_IGN);
72+
signal(SIGCHLD, SIG_IGN); /* will save one syscall per sleep */
73+
signal(SIGTERM, sigterm);
74+
75+
if ( (net = fopen("/proc/net/dev", "r")) == NULL) {
76+
perror("fopen");
77+
exit(-1);
78+
}
79+
80+
if ( (stat = fopen("/proc/stat", "r")) == NULL) {
81+
perror("fopen");
82+
exit(-1);
83+
}
84+
85+
StatsdClient client = StatsdClient(argv[1], 8125, ns);
86+
87+
daemon(0,0);
88+
89+
while(running) {
90+
sysinfo(&si);
91+
client.send("load", 100*si.loads[0]/0x10000, "g", 1.0);
92+
client.send("freemem", si.freeram/1024, "g", 1.0);
93+
client.send("procs", si.procs, "g", 1.0);
94+
client.send("uptime", si.uptime, "c", 1.0);
95+
96+
rewind(net);
97+
while(!feof(net)) {
98+
fgets(line, MAX_LINE_LEN, net);
99+
if (!strstr(line, iface)) {
100+
continue;
101+
}
102+
i = 0;
103+
for (i = 0, p = strtok_r(line, " ", &lasts); p;
104+
p = strtok_r(NULL, " ", &lasts), i++) {
105+
if (i == 1) {
106+
client.send("if-rx", atoi(p), "c", 1.0);
107+
} else if (i == 9) {
108+
client.send("if-tx", atoi(p), "c", 1.0);
109+
break;
110+
}
111+
}
112+
}
113+
114+
/* rewind doesn't do the trick for /proc/stat */
115+
freopen("/proc/stat", "r", stat);
116+
fgets(line, MAX_LINE_LEN, stat);
117+
sscanf(line, "cpu %u %u %u %u", &user, &nice, &sys, &idle);
118+
total = user + sys + idle;
119+
busy = user + sys;
120+
121+
client.send("cpu", 100 * (busy - old_busy)/(total - old_total), "g", 1.0);
122+
123+
old_total = total;
124+
old_busy = busy;
125+
sleep(6);
126+
}
127+
128+
fclose(net);
129+
fclose(stat);
130+
131+
exit(0);
132+
}

demo/test_client.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
#include <iostream>
3+
#include <unistd.h>
4+
#include "statsd_client.h"
5+
using namespace statsd;
6+
7+
int main(void)
8+
{
9+
std::cout << "running..." << std::endl;
10+
11+
StatsdClient client("127.0.0.1", 8125);
12+
StatsdClient client2("127.0.0.1", 8125, "myproject.abx.");
13+
14+
client.count("count1", 123, 1.0);
15+
client.count("count2", 125, 1.0);
16+
client.gauge("speed", 10);
17+
client2.timing("request", 2400);
18+
sleep(1);
19+
client.inc("count1", 1.0);
20+
client2.dec("count2", 1.0);
21+
int i;
22+
for(i=0; i<10; i++) {
23+
client2.count("count3", i, 0.8);
24+
}
25+
26+
std::cout << "done" << std::endl;
27+
return 0;
28+
}

src/Makefile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
CXX=g++
3+
CPPFLAGS += -Wall -g
4+
5+
TARGET = statsd_client
6+
TARGET_LIB = lib$(TARGET).a
7+
TARGET_HEADER = $(TARGET).h
8+
TARGET_SOURCE = $(TARGET).cpp
9+
TARGET_OBJ = $(TARGET).o
10+
11+
all: $(TARGET_LIB)
12+
13+
$(TARGET_LIB): $(TARGET_OBJ)
14+
ar crus $@ $<
15+
16+
$(TARGET_OBJ): $(TARGET_SOURCE) $(TARGET_HEADER)
17+
$(CXX) -c -o $@ $< $(CPPFLAGS)
18+
19+
clean:
20+
rm -f $(TARGET_OBJ) $(TARGET_LIB)
21+

0 commit comments

Comments
 (0)