forked from lensen/graphsky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
282 lines (251 loc) · 10.1 KB
/
functions.php
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
#------------------------------------------------------------------------------
# Functions to make strings compatible
function clean_string( $string ) {
return htmlentities( $string );
}
function sanitize ( $string ) {
return escapeshellcmd( clean_string( rawurldecode( $string ) ) ) ;
}
function sanitize_datetime ( $dt ) {
if (preg_match("/^(\d{4}[-\/]\d{2}[-\/]\d{2}|\d{2}[-\/]\d{2}[-\/]\d{4}) \d{1,2}:\d{2}/", $dt)) {
return date('H:i_Ymd', strtotime($dt));
}
else {
return $dt;
}
}
#------------------------------------------------------------------------------
# Function to print options for dropdown menus
function print_dropdown_menus($options, $choice, $default) {
if ( $default != "" ) {
$option_values = " <option value=\"\">$default</option>\n";
}
else {
$option_values = "";
}
foreach ($options as $option) {
if ($option == $choice) {
$selected = "selected=\"selected\"";
}
else {
$selected = "";
}
$option_values .= " <option value=\"$option\" $selected>$option</option>\n";
}
return $option_values;
}
#------------------------------------------------------------------------------
# Function to return graph domainname.
function get_graph_domainname() {
global $conf;
if ($conf['use_random_graph_domainname'])
return str_replace("//", "//img" . rand(10,80) . ".", $conf['graph_domainname']);
return ""; #$conf['graph_domainname'];
}
#------------------------------------------------------------------------------
# Function to build a Graphite url based on a json report.
function build_graphite_series( $config, $host_cluster = "" ) {
$targets = array();
$colors = array();
// Keep track of stacked items
$stacked = 0;
$pie = 0;
if ( isset($config['units']) )
$units = $config['units'];
else
$units = "si";
foreach( $config[ 'series' ] as $item ) {
$functions = array();
if ( $item['type'] == "stack" )
$stacked++;
if ( $item['type'] == "pie" )
$pie++;
if ( isset($item['functions']) )
$functions = $item['functions'];
else
$functions[0] = "sumSeries";
if ( isset($item['hostname']) && isset($item['clustername']) )
$host_cluster = $item['clustername'] . "." . str_replace(".","_", $item['hostname']);
$metric = "$host_cluster.${item['metric']}";
foreach( $functions as $function ) {
$metric = "$function($metric)";
}
# $targets[] = "target=". urlencode( "cactiStyle(alias($metric,'${item['label']}'),'${units}')" );
$targets[] = "target=". urlencode( "alias($metric,'${item['label']}')" );
$colors[] = $item['color'];
}
$output = implode( $targets, '&' );
$output .= "&colorList=" . implode( $colors, ',' );
$output .= "&vtitle=" . urlencode( isset($config[ 'vertical_label' ]) ? $config[ 'vertical_label' ] : "" );
$output .= "&yUnitSystem=" . $units;
if ( isset($config['graph_max']) )
$output .= "&max=" . $config['graph_max'];
if ( $stacked > 0 ) {
if ( $stacked > 1 )
$output .= "&areaMode=stacked";
else
$output .= "&areaMode=first";
}
elseif ( $pie > 0 ) {
$output .= "&graphType=pie";
}
return $output;
}
#------------------------------------------------------------------------------
# Functions for printing graph (cards)
function print_graph($args, $metric_report, $graph_size, $from, $until) {
global $conf;
$width = $conf['graph_sizes'][$graph_size]['width'];
$height = $conf['graph_sizes'][$graph_size]['height'];
$graph_html = "
<div class=\"graph_card\">
<div class=\"graph_img\">
<a href=\"?$args&z=$graph_size&from=$from&until=$until\">
<img width=\"$width\" height=\"$height\" class=\"lazy\" src=\"img/blank.gif\" data-original=\"". get_graph_domainname() . "/graph.php?$args&$metric_report&z=$graph_size&from=$from&until=$until\" />
</a>
</div>
" . show_graph_buttons("$args&$metric_report", $from, $until) . "</div>";
return $graph_html;
}
function print_zoom_graph($args, $metric_report, $graph_size, $from, $until) {
global $conf;
$width = $conf['graph_sizes'][$graph_size]['width'];
$height = $conf['graph_sizes'][$graph_size]['height'];
$graph_html = "
<div class=\"graph_card\">
<div class=\"graph_img\">
<a href=\"graph.php?$args&$metric_report&from=$from&until=$until&z=xlarge\">
<img width=\"$width\" height=\"$height\" class=\"lazy\" src=\"img/blank.gif\" data-original=\"". get_graph_domainname() . "/graph.php?$args&$metric_report&z=$graph_size&from=$from&until=$until\" />
</a>
</div><div class=\"graph_name left\">$metric_report</div>
" . show_graph_buttons("$args&$metric_report", $from, $until) . "</div>";
return $graph_html;
}
function show_graph_buttons($args, $from, $until) {
global $conf;
$button_html = "<div class=\"graph_buttons right\">
<a href=\"graph_all_periods.php?$args\">
<img src=\"img/historical.png\" class=\"graph_button\" title=\"Show periodic graphs\">
</a>
<a href=\"graph.php?$args&from=$from&until=$until&z=xlarge\">
<img src=\"img/zoom.png\" class=\"graph_button\" title=\"Show XL graph\">
</a> ";
if (isset($conf['graphlot_url_base'])) {
$button_html = $button_html . "
<a href=\"graph.php?$args&from=$from&until=$until&graphlot=true\" target=\"_blank\">
<img src=\"img/graphlot.png\" class=\"graph_button\" title=\"Show Graphlot\">
</a>";
}
$button_html = $button_html . "
</div>
";
return $button_html;
}
function print_period_graph($args, $timeframe) {
global $conf;
$graph_size = "large";
$width = $conf['graph_sizes']["$graph_size"]['width'];
$height = $conf['graph_sizes']["$graph_size"]['height'];
$graph_html = "
<div class=\"graph_card\">
<div class=\"graph_img\">
<a href=\"graph.php?$args&z=xlarge&st=$timeframe+ago\">
<img width=\"$width\" height=\"$height\" class=\"lazy\" src=\"img/blank.gif\" data-original=\"". get_graph_domainname() . "/graph.php?$args&z=$graph_size&st=$timeframe+ago\" />
</a>
</div>
</div>
";
return $graph_html;
}
#------------------------------------------------------------------------------
# Finds the max over a set of metric graphs.
function find_limits($environment, $cluster, $metricname, $start, $end) {
global $conf;
$max=0;
$target = $conf['graphite_prefix'] . "$environment.$cluster.*." . $metricname;
$raw_data = file_get_contents($conf['graphite_render_url'] . "?target=$target&from=$start&until=$end&format=json");
$data = json_decode($raw_data, TRUE);
$maxdatapoints = array();
foreach ( $data as $data_target ) {
$highestMaxDatapoints = $data_target['datapoints'];
foreach ( $highestMaxDatapoints as $datapoint ) {
$maxdatapoints[] = $datapoint[0];
}
}
sort($maxdatapoints);
$max = round(max($maxdatapoints) * 1.1);
return $max;
}
#------------------------------------------------------------------------------
# Finds dashboards to specific environment/cluster
function find_dashboards($environment, $cluster="") {
global $conf, $dash_config;
if ( ! isset( $dash_config ) )
$dash_config = json_decode(file_get_contents($conf['dashboard_config']), TRUE);
$graph_reports = array();
foreach ($dash_config['dashboards'] as $dash) {
if (! preg_match($dash['environments'], $environment) ) {
continue;
}
if ($cluster) {
if (! preg_match($dash['clusters'], $cluster)){
continue;
}
}
foreach ($dash['included_reports'] as $dashboard) {
$graph_reports[] = $dashboard;
}
}
return $graph_reports;
}
#------------------------------------------------------------------------------
# Determines of report graphs should be shows in this dashboard
function show_on_dashboard($report_name, $environment, $cluster) {
global $conf, $dash_config;
if ( ! isset( $dash_config ) )
$dash_config = json_decode(file_get_contents($conf['dashboard_config']), TRUE);
foreach ($dash_config['dashboards'] as $dash) {
if ( preg_match($dash['environments'], $environment) && preg_match($dash['clusters'], $cluster) ){
if ( in_array($report_name, $dash['included_reports']) ) {
return True;
}
}
}
return False;
}
#------------------------------------------------------------------------------
# Find graphite metrics matching regex
function find_metrics($search_string, $group_depth=0) {
global $conf;
$metrics = array();
$search_url = $conf['graphite_url_base'] . "/metrics/expand/?leavesOnly=1";
$search_prefix = quotemeta($conf['graphite_prefix'] . $search_string);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$search_query_array = array();
$search_query_wildcard = "";
$i = 0;
while ($i <= 10) {
$search_query_wildcard = $search_query_wildcard . ".*";
$search_query_item = "&query=" . $search_prefix . $search_query_wildcard;
$search_query_array[] = $search_query_item;
$i++;
}
$search_query = implode("", $search_query_array);
curl_setopt($ch, CURLOPT_URL, $search_url . $search_query);
$search_result = json_decode(curl_exec($ch), TRUE);
$results = $search_result['results'];
natsort($results);
foreach ($results as $metric) {
$metric_string = preg_replace("/^$search_prefix\./", "", $metric);
$arr = explode('.',trim($metric_string));
$metric_group = join(".", array_slice($arr, 0, $group_depth));
if (!isset($metrics[$metric_group]) )
$metrics[$metric_group] = array();
$metrics[$metric_group][] = $metric_string;
}
curl_close($ch);
return $metrics;
}