forked from unbit/uwsgi.it
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector.pl
214 lines (180 loc) · 5.21 KB
/
collector.pl
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use bigint;
use LWP::UserAgent;
use JSON -support_by_pp;
use Config::IniFiles;
use POSIX qw(strftime);
use IO::Socket::INET;
use Quota;
# required for --log-master
STDOUT->autoflush(1);
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;
my $cfg = Config::IniFiles->new( -file => "/etc/uwsgi/local.ini" );
my $base_url = 'https://'.$cfg->val('uwsgi', 'api_domain').'/api/private';
my $ssl_key = $cfg->val('uwsgi', 'api_client_key_file');
my $ssl_cert = $cfg->val('uwsgi', 'api_client_cert_file');
my $root_dev = get_mountpoint('/');
sub collect_metrics {
my ($uid, $net_json) = @_;
collect_metrics_cpu($uid);
collect_metrics_io($uid);
collect_metrics_mem($uid);
collect_metrics_quota($uid);
if ($net_json) {
collect_metrics_net($uid, $net_json);
}
}
sub collect_metrics_cpu {
my ($uid) = @_;
open CGROUP,'/sys/fs/cgroup/'.$uid.'/cpuacct.usage';
my $value = <CGROUP>;
close CGROUP;
chomp $value;
push_metric($uid, 'container.cpu', $value);
}
sub collect_metrics_mem {
my ($uid) = @_;
open CGROUP,'/sys/fs/cgroup/'.$uid.'/memory.usage_in_bytes';
my $value = <CGROUP>;
close CGROUP;
chomp $value;
push_metric($uid, 'container.mem', $value);
}
sub collect_metrics_quota {
my ($uid) = @_;
my ($blocks) = Quota::query($root_dev, $uid);
push_metric($uid, 'container.quota', Math::BigInt->new($blocks) * 1024);
}
sub collect_metrics_net {
my ($uid, $tuntap) = @_;
my $peers = $tuntap->{peers};
foreach(@{$peers}) {
if ($_->{uid} == $uid) {
push_metric($uid, 'container.net.rx', $_->{rx});
push_metric($uid, 'container.net.tx', $_->{tx});
return;
}
}
}
sub collect_metrics_io {
my ($uid) = @_;
my $r = Math::BigInt->new('0');
my $w = Math::BigInt->new('0');
open CGROUP,'/sys/fs/cgroup/'.$uid.'/blkio.io_service_bytes';
while(<CGROUP>) {
chomp;
my ($device, $type, $value) = split /\s+/;
if ($type eq 'Read') {
my $r0 = Math::BigInt->new($value);
$r += $r0;
}
elsif ($type eq 'Write') {
my $w0 = Math::BigInt->new($value);
$w += $w0;
}
}
close CGROUP;
push_metric($uid, 'container.io.read', $r);
push_metric($uid, 'container.io.write', $w);
}
for(;;) {
my $ua = LWP::UserAgent->new;
$ua->ssl_opts(
SSL_key_file => $ssl_key,
SSL_cert_file => $ssl_cert,
);
$ua->timeout(3);
my $response = $ua->get($base_url.'/containers/');
if ($response->is_error or $response->code != 200 ) {
print date().' oops: '.$response->code.' '.$response->message."\n";
exit;
}
my $containers = decode_json($response->decoded_content);
my $net_json = undef;
# get json stats from the tuntap router
my $s = IO::Socket::INET->new(PeerAddr => '127.0.0.1:5002');
if ($s) {
my $tuntap_json = '';
for(;;) {
$s->recv(my $buf, 8192);
last unless $buf;
$tuntap_json .= $buf;
}
$net_json = decode_json($tuntap_json);
}
foreach(@{$containers}) {
collect_metrics($_->{uid}, $net_json);
}
my $d_json = undef;
# now collect domains metrics
my $s = IO::Socket::INET->new(PeerAddr => '127.0.0.1:5003');
if ($s) {
my $domains_json = '';
for(;;) {
$s->recv(my $buf, 8192);
last unless $buf;
$domains_json .= $buf;
}
$d_json = decode_json($domains_json);
}
foreach(@{$d_json->{subscriptions}}) {
my $domain = $_->{key};
my $nodes = $_->{nodes};
foreach(@{$nodes}) {
if ($_->{uid} > 30000) {
push_domain_metric($_->{uid}, $domain, 'domain.net.rx', $_->{rx});
push_domain_metric($_->{uid}, $domain, 'domain.net.tx', $_->{rx});
push_domain_metric($_->{uid}, $domain, 'domain.hits', $_->{requests});
}
}
}
# gather metrics every 5 minutes
sleep(300);
}
sub date {
return strftime "%Y-%m-%d %H:%M:%S", localtime;
}
sub push_metric {
my ($uid, $path, $value) = @_;
my $ua = LWP::UserAgent->new;
$ua->ssl_opts(
SSL_key_file => $ssl_key,
SSL_cert_file => $ssl_cert,
);
$ua->timeout(3);
my $j = JSON->new;
$j->allow_bignum(1);
$j = $j->encode({unix => time, value => Math::BigInt->new($value)});
my $response = $ua->post($base_url.'/metrics/'.$path.'/'.$uid, Content => $j);
if ($response->is_error or $response->code != 201 ) {
print date().' oops for '.$path.'/'.$uid.': '.$response->code.' '.$response->message."\n";
}
}
sub push_domain_metric {
my ($uid, $domain, $path, $value) = @_;
my $ua = LWP::UserAgent->new;
$ua->ssl_opts(
SSL_key_file => $ssl_key,
SSL_cert_file => $ssl_cert,
);
$ua->timeout(3);
my $j = JSON->new;
$j->allow_bignum(1);
$j = $j->encode({domain => $domain, unix => time, value => Math::BigInt->new($value)});
my $response = $ua->post($base_url.'/metrics/'.$path.'/'.$uid, Content => $j);
if ($response->is_error or $response->code != 201 ) {
print date().' oops for '.$path.'/'.$uid.': '.$response->code.' '.$response->message."\n";
}
}
sub get_mountpoint {
my ($mountpoint) = @_;
open MOUNTS,'/proc/self/mounts';
while(<MOUNTS>) {
my ($dev, $mp) = split /\s/;
if ($mp eq $mountpoint) {
if ($dev ne 'rootfs') {
return $dev;
}
}
}
return '';
}