-
Notifications
You must be signed in to change notification settings - Fork 0
/
collect_and_push.pl
executable file
·198 lines (158 loc) · 6.48 KB
/
collect_and_push.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
#!/usr/bin/perl
use strict;
use warnings;
use Time::HiRes qw(time);
use WWW::Curl::Easy;
use MIME::Base64 ();
my $GLOBAL_TIMESTAMP;
my $KUBERNETES_TOKEN;
my $KUBERNETES_ADDRESS;
my $KUBERNETES_PORT;
my $LOG_LEVEL=0;
my $NODE_NAME=$ARGV[0] or die ("No node name associated");
my $NODE_ALLOCATABLE_CPU=$ARGV[1] or die ("No node allocatable CPU information associated");
my $NODE_ALLOCATABLE_MEMORY=$ARGV[2] or die ("No node allocatable Memory information associated");
my $NODE_ALLOCATABLE_DISK=$ARGV[3] or die ("No node allocatable Disk information associated");
sub validateEnvironmentVariables{
if(!defined $ENV{'KUBERNETES_CUSTOM_TOKEN'}){
my $filename = '/var/run/secrets/kubernetes.io/serviceaccount/token';
open(my $fh, '<', $filename)
or die "Could not open file '$filename' $! Are you sure you are in a Kubernetes environment?";
my $token = <$fh>;
if( $token ne "" ){
$KUBERNETES_TOKEN = $token;
} else {
die("Kubernetes environment variables are not set correctly and couldn't find any token in /var/run/secrets/kubernetes.io/serviceaccount/token!");
}
} else{
$KUBERNETES_TOKEN = $ENV{'KUBERNETES_CUSTOM_TOKEN'};
}
if(!defined $ENV{'KUBERNETES_CUSTOM_ADDRESS'}){
if(!defined $ENV{'KUBERNETES_PORT_443_TCP_ADDR'}){
die("Both KUBERNETES_ADDRESS and KUBERNETES_PORT_443_TCP_ADDR is not set! Are you sure you are in a Kubernetes environment?");
} else{
$KUBERNETES_ADDRESS = $ENV{'KUBERNETES_PORT_443_TCP_ADDR'};
}
} else{
$KUBERNETES_ADDRESS = $ENV{'KUBERNETES_CUSTOM_ADDRESS'};
}
if(!defined $ENV{'KUBERNETES_CUSTOM_PORT'}){
if(!defined $ENV{'KUBERNETES_PORT_443_TCP_PORT'}){
die("Both KUBERNETES_PORT and KUBERNETES_PORT_443_TCP_PORT is not set! Are you sure you are in a Kubernetes environment?");
} else{
$KUBERNETES_PORT = $ENV{'KUBERNETES_PORT_443_TCP_PORT'};
}
} else{
$KUBERNETES_PORT = $ENV{'KUBERNETES_CUSTOM_PORT'};
}
if(!defined $ENV{'INFLUXDB_ADDRESS'} || !defined $ENV{'INFLUXDB_PORT'} || !defined $ENV{'INFLUXDB_USERNAME'} || !defined $ENV{'INFLUXDB_PASSWORD'} || !defined $ENV{'INFLUXDB_DATABASE'}){
die("InfluxDB environment variables are not set correctly!");
}
# if(!defined $ENV{'NODE_NAME'}){
# die("\$NODE_NAME is not set!");
# }
if(defined $ENV{'LOG_LEVEL'}){
$LOG_LEVEL=$ENV{'LOG_LEVEL'};
}
return 1;
}
sub printKubernetesEnvironmentVariables{
print("\$KUBERNETES_TOKEN=$KUBERNETES_TOKEN\n\$KUBERNETES_ADDRESS=$KUBERNETES_ADDRESS\n\$KUBERNETES_PORT=$KUBERNETES_PORT\n");
}
sub getMetrics{
my $curl = WWW::Curl::Easy->new;
my @headers = ("Authorization: Bearer $KUBERNETES_TOKEN");
my $URL = "https://$KUBERNETES_ADDRESS:$KUBERNETES_PORT/api/v1/nodes/$NODE_NAME/proxy/metrics/cadvisor";
$curl->setopt(CURLOPT_HEADER,1);
$curl->setopt(CURLOPT_HTTPHEADER, \@headers);
$curl->setopt(CURLOPT_SSL_VERIFYPEER, 0);
$curl->setopt(CURLOPT_SSL_VERIFYHOST, 0);
$curl->setopt(CURLOPT_URL, $URL);
my $response_body;
$curl->setopt(CURLOPT_WRITEDATA,\$response_body);
my $retcode = $curl->perform;
$GLOBAL_TIMESTAMP=sprintf("%.9f",time);
$GLOBAL_TIMESTAMP=~s/\.//g;
if ($retcode == 0) {
if( $LOG_LEVEL > 0 ){
print("Metrics request/response succeed!\n");
}
my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
if( $response_code != 200 ){
die("Error with response code: $response_code\n");
}
} else {
die("An error happened: $retcode ".$curl->strerror($retcode)." ".$curl->errbuf."\n");
}
return $response_body;
}
sub generateQuery{
my $response_body = $_[0];
my $query = "";
foreach my $line (split /\n/ ,$response_body) {
for ($line) {
if (/^([A-Za-z0-9_-]+?)\{(.*?)\}\s+(.*?)$/) {
my $measurement = $1;
my $val = sprintf("%.20g", $3);
my $metadata = $2;
$metadata =~ s/\s/_/g;
$query .= "$measurement,node_name=$NODE_NAME,$metadata value=$val $GLOBAL_TIMESTAMP\n";
} elsif(/^([A-Za-z0-9_-]+?)\s+(.*?)$/) {
my $val = sprintf("%.20g", $2);
$query .= "$1,node_name=$NODE_NAME value=$val $GLOBAL_TIMESTAMP\n";
}
}
}
$query .= "node_allocatable_cpu,node_name=$NODE_NAME value=$NODE_ALLOCATABLE_CPU $GLOBAL_TIMESTAMP\n";
$query .= "node_allocatable_memory,node_name=$NODE_NAME value=$NODE_ALLOCATABLE_MEMORY $GLOBAL_TIMESTAMP\n";
$query .= "node_allocatable_disk,node_name=$NODE_NAME value=$NODE_ALLOCATABLE_DISK $GLOBAL_TIMESTAMP\n";
return $query;
}
sub pushToInfluxDB{
my $query = $_[0];
my $curl = WWW::Curl::Easy->new;
my $URL = "http://$ENV{'INFLUXDB_ADDRESS'}:$ENV{'INFLUXDB_PORT'}/write?db=$ENV{'INFLUXDB_DATABASE'}";
if( $ENV{'INFLUXDB_USERNAME'} ne "" && $ENV{'INFLUXDB_PASSWORD'} ne "" ){
my @headers = ("Authorization: Basic ".MIME::Base64::encode($ENV{'INFLUXDB_USERNAME'}.":".$ENV{'INFLUXDB_PASSWORD'}));
$curl->setopt(CURLOPT_HTTPHEADER, \@headers);
}
$curl->setopt(CURLOPT_POST,1);
$curl->setopt(CURLOPT_POSTFIELDS,$query);
$curl->setopt(CURLOPT_HEADER,1);
$curl->setopt(CURLOPT_SSL_VERIFYPEER, 0);
$curl->setopt(CURLOPT_SSL_VERIFYHOST, 0);
$curl->setopt(CURLOPT_URL, $URL);
my $response_body;
$curl->setopt(CURLOPT_WRITEDATA,\$response_body);
my $retcode = $curl->perform;
$GLOBAL_TIMESTAMP=sprintf("%.9f",time);
$GLOBAL_TIMESTAMP=~s/\.//g;
if ($retcode == 0) {
if( $LOG_LEVEL > 0 ){
print("InfluxDB request/response succeed!\n");
}
my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
if( $response_code != 200 && $response_code != 204 ){
die("Response error from InfluxDB ($URL): Error with response code: $response_code\n");
}
} else {
die("Error communicating with InfluxDB: An error happened: $retcode ".$curl->strerror($retcode)." ".$curl->errbuf."\n");
}
return 1;
}
if( validateEnvironmentVariables() ){
if( $LOG_LEVEL > 1 ){
printKubernetesEnvironmentVariables();
}
my $response_body = getMetrics();
my $query = generateQuery($response_body);
if( $LOG_LEVEL > 1 ){
open(my $fh, '>', 'query.log');
print $fh $query;
close $fh;
}
my $response = pushToInfluxDB($query);
if( $response && $LOG_LEVEL > 0 ){
print("Metrics for $NODE_NAME successfully pushed to InfluxDB at http://$ENV{'INFLUXDB_ADDRESS'}:$ENV{'INFLUXDB_PORT'}/write?db=$ENV{'INFLUXDB_DATABASE'}!\n");
}
}