-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWeatherMapDataSource_prometheus_manualtest.php
More file actions
186 lines (147 loc) · 5.53 KB
/
WeatherMapDataSource_prometheus_manualtest.php
File metadata and controls
186 lines (147 loc) · 5.53 KB
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
<?php
// Assuming the prometheus query is returning a single result, in bits/second.
// Query example I use to monitor the bw on a Cisco Switch:
// ` irate(ifHCInOctets{ifName="Gi0/1",instance="192.168.178.32"}[2m])*8 `
//
// irate()[2m] => delta between the last 2 points in the serie, I look
// in serie up to 2mins in the past to find them.
// instance => the switch I want to check the interface in ( ifName )
//$prom_query = 'prometheus:http:localhost:9090:192.168.178.32:Gi0/13:ifHCInOctets:ifHCOutOctets';
//$prom_query = 'prometheus:http:localhost:9090:sum(irate(ifHCInOctets{ifAlias=~".*ae232.*",instance="192.168.178.32"}[10m]))*8:sum(irate(ifHCOutOctets{ifAlias=~".*ae232.*",instance="192.168.178.32"}[10m]))*8';
//$prom_query = 'prometheus:http:localhost:9090:max(irate(ifHCInOctets{ifAlias=~".*ae232.*",instance="192.168.178.32"}[10m]))*8:max(irate(ifHCOutOctets{ifAlias=~".*ae232.*",instance="192.168.178.32"}[10m]))*8';
$prom_query = 'prometheus:http:localhost:9090:192.168.178.32:Gi0/13:ifHCInOctets:ifHCOutOctets';
// regexes for single / dual value prom query
$prom_regex_single_value = '/^prometheus\:(http|https)\:([a-zA-z0-9-_.]+)\:(\d+)\:([a-zA-z0-9-_.]+)\:([^:]+)\:([^:]+)$/';
$prom_regex_dual_value = '/^prometheus\:(http|https)\:([a-zA-z0-9-_.]+)\:(\d+)\:([a-zA-z0-9-_.]+)\:([^:]+)\:([^:]+)\:([^:]+)$/';
$prom_regex_single_query = '/^prometheus\:(http|https)\:([a-zA-z0-9-_.]+)\:(\d+)\:([^:]+)$/';
$prom_regex_dual_query = '/^prometheus\:(http|https)\:([a-zA-z0-9-_.]+)\:(\d+)\:([^:]+)\:([^:]+)$/';
// Parse the plugin line and recognise prom plugin
function Recognise($targetstring)
{
global $prom_regex_single_value, $prom_regex_dual_value, $prom_regex_single_query, $prom_regex_dual_query;
if( preg_match($prom_regex_single_value,$targetstring,$matches) ||
preg_match($prom_regex_dual_value,$targetstring,$matches) ||
preg_match($prom_regex_single_query,$targetstring,$matches) ||
preg_match($prom_regex_dual_query,$targetstring,$matches) )
{
return TRUE;
}
else
{
return FALSE;
}
}
function ReadData($targetstring)
{
global $prom_regex_single_value, $prom_regex_dual_value, $prom_regex_single_query, $prom_regex_dual_query;
$inbw = NULL;
$outbw = NULL;
$data_time=0;
if(preg_match($prom_regex_dual_value,$targetstring,$matches))
{
// In and Out query provided
$proto = $matches[1];
$remote_host = $matches[2];
$remote_port = $matches[3];
$instance = $matches[4];
$intf = $matches[5];
$in_series = $matches[6];
$out_series = $matches[7];
$url = $proto . '://' . $remote_host . ':' . $remote_port . '/api/v1/query?query=';
$in_query = 'irate(' . $in_series . '{ifName="' . $intf . '",instance="' . $instance . '"}[2m])*8';
$out_query = 'irate(' . $out_series . '{ifName="' . $intf . '",instance="' . $instance . '"}[2m])*8';
// IN
$query = urlencode($in_query);
$call = $url . $query;
$inbw = GetPromData($call);
// OUT
$query = urlencode($out_query);
$call = $url . $query;
$outbw = GetPromData($call);
}
elseif(preg_match($prom_regex_single_value,$targetstring,$matches))
{
// Only In query provided
$proto = $matches[1];
$remote_host = $matches[2];
$remote_port = $matches[3];
$instance = $matches[4];
$intf = $matches[5];
$in_series = $matches[6];
$url = $proto . '://' . $remote_host . ':' . $remote_port . '/api/v1/query?query=';
$in_query = 'irate(' . $in_series . '{ifName="' . $intf . '",instance="' . $instance . '"}[2m])*8';
$query = urlencode($in_query);
$call = $url . $query;
$inbw = GetPromData($call);
$outbw = $inbw;
}
elseif(preg_match($prom_regex_dual_query,$targetstring,$matches))
{
// In and Out as free Prometheus queries
$proto = $matches[1];
$remote_host = $matches[2];
$remote_port = $matches[3];
$in_query = $matches[4];
$out_query = $matches[5];
$url = $proto . '://' . $remote_host . ':' . $remote_port . '/api/v1/query?query=';
// IN
$query = urlencode($in_query);
$call = $url . $query;
$inbw = GetPromData($call);
// OUT
$query = urlencode($out_query);
$call = $url . $query;
$outbw = GetPromData($call);
}
elseif(preg_match($prom_regex_single_query,$targetstring,$matches))
{
// In and Out as free Prometheus queries
$proto = $matches[1];
$remote_host = $matches[2];
$remote_port = $matches[3];
$in_query = $matches[4];
$url = $proto . '://' . $remote_host . ':' . $remote_port . '/api/v1/query?query=';
$query = urlencode($in_query);
$call = $url . $query;
$inbw = GetPromData($call);
$outbw = $inbw;
}
$data_time = time();
return ( array($inbw,$outbw,$data_time) );
}
function GetPromData($url)
{
// this is wget like :)
$content = file_get_contents($url);
if ($content)
{
$json_data = json_decode($content, true);
if (isset($json_data["data"]["result"][0]["value"][1]))
{
$bw = round(intval($json_data["data"]["result"][0]["value"][1]));
}
else
{
$bw = NULL;
}
}
else
{
$bw = NULL;
}
return $bw;
}
// Start working
$valid_request = Recognise($prom_query);
if ($valid_request)
{
echo "The Request is valid\n";
}
else
{
echo "The Request is invalid\n";
}
$output = ReadData($prom_query);
print_R($output);
?>
// vim:ts=4:sw=4: