-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_graph_panel.php
127 lines (120 loc) · 5.03 KB
/
create_graph_panel.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
<?php
// include_once("lib/validate_session.php") ;
include_once( "open_lib/class.TemplatePower.inc.php" );
include ( "lib/parse_xml.php") ;
include( "lib/ganglia_config.php") ;
$xml_ob = new parse_xml () ;
$xml_ob->parse() ;
// Build a hash of query string , $_GET is not working with mutiple options
$a = explode ( '&' , $_SERVER["QUERY_STRING"] ) ;
$query = array() ;
foreach ( $a as $q ) {
list($key, $value) = explode("=", $q);
if ( array_key_exists( $key , $query ) ) {
array_push( $query["$key"] , $value ) ;
} else {
$query["$key"] = array() ;
array_push( $query["$key"] , $value ) ;
}
}
$config_ob = new ganglia_config() ;
$globals = $config_ob->global_config () ;
$API_URL = $_SERVER["SERVER_NAME"] . "/" . $globals["api_url"] . "?";
/* A. ) first determine which cluster are we graphing right now .
* if the cluster is "All"
** Get list of all metrics If the server list is also all
** Get list of all metrics if the servers list is not all by going though each server's metrics
* if the cluster is not "All"
** Take a particular server and get all metrics list of it
** Get all metrics group name
** Create graphs based on the grouping decided
*/
// If metrics groups is "All" , Find all the metrics groups of this cluster
$cluster = array_pop ( $query["cluster"] );
if ( preg_match("/^all$/i" , $cluster ) ) {
// Selection is of hosts
$url_grp = $API_URL . "list=metrics_grp" ;
$url_metrics = $API_URL . "list=metrics" ;
$url_servers = $API_URL . "list=servers" ;
} else {
$url_grp = $API_URL . "list=metrics_grp&clusters=" . $cluster ;
$url_metrics = $API_URL . "list=metrics&clusters=" . $cluster ;
$url_servers = $API_URL . "list=servers&clusters=". $cluster ;
}
$ret_content = get_data($url_grp) ;
$json = json_decode($ret_content , true) ;
$metrics_groups = $json["$cluster"] ;
if ( ! preg_match("/^all$/i" , $query["metrics_group"][0] ) ) {
$metrics_groups = $query["metrics_group"] ;
}
$ret_content = get_data($url_metrics) ;
$json = json_decode($ret_content , true) ;
$metrics = $json["$cluster"] ;
$ret_content = get_data($url_servers) ;
$json = json_decode($ret_content , true) ;
$servers = array() ;
$servers_key = "" ; // list to be passed in GET request
if ( ( isset($query["servers"]) ) && ( in_array( "All" , $query["servers"] ) ) ) {
$servers = $json["$cluster"] ;
} else if ( ! isset($query["servers"]) ) {
$servers = $json["$cluster"] ;
} else {
$servers = $query["servers"] ;
}
// We have list of servers , metrics , metrics group , cluster to be graphed .
$panel_tpl = init_template("templates/default/panel.tpl") ;
$i = 0 ;
$metric = "all" ;
if ( isset ( $query["metrics"] ) && ( ! preg_match( "/^all$/i" , $query["metrics"][0] ) ) ) {
$metric = $query["metrics"][0] ;
if ( preg_match("/^all$/i" , $query["metrics_group"][0] ) ) {
$metrics_groups = array() ;
array_push ( $metrics_groups , "$metric" ) ;
}
}
// Generate options query
$config = $config_ob->all_config() ;
$option = '';
foreach ( $config["graph_defaults"] as $name => $value ) {
if ( isset ( $query[$name] ) ) {
$option .= '&' . $name . "=" . $query[$name][0] ;
} else if ( $name != "vertical-label" ) {
$option .= '&' . $name . "=" . $value ;
}
}
foreach ( $metrics_groups as $metric_grp ) {
if ( ! preg_match("/system|core/i" , $metric_grp) ) {
$i += 1;
$id_group = $metric_grp ;
$id_group = preg_replace( '/[+ ]/' , '_' , $id_group ) ; // Dirty hack , jquery form plugin send + in query instead of %20
$panel_tpl->newBlock("group") ;
$panel_tpl->assign("group" , $id_group ) ;
$panel_tpl->newBlock("ajax_req") ;
$panel_tpl->assign("group" , $id_group ) ;
$panel_tpl->assign("group_un" , $metric_grp ) ;
$panel_tpl->assign("servers" , implode(',' , $servers ) ) ;
$panel_tpl->assign("metric" , $metric ) ;
$panel_tpl->assign("cluster" , $cluster ) ;
$panel_tpl->assign("options" , $option ) ;
}
}
$panel_tpl->printToScreen() ;
/* gets the data from a URL */
function init_template( $file ) {
$tpl = new TemplatePower("$file") ;
$tpl->prepare() ;
return $tpl ;
}
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, false );
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>