This repository has been archived by the owner on Dec 22, 2019. It is now read-only.
/
bandwidth.php
116 lines (111 loc) · 3.54 KB
/
bandwidth.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
<?
if(!isset($_GET["load"])) exit;
require_once('/home/frozen/phpcassa.php');
function get_date_format($period) {
$formats = array(
"monthly" => "Y-m",
"daily" => "Y-m-d",
"hourly" => "Y-m-d.H"
);
if(array_key_exists($period, $formats))
return $formats[$period];
return "";
}
function get_date_step($period) {
$steps = array(
"monthly" => "+1 month",
"daily" => "+1 day",
"hourly" => "+1 hour",
);
if(array_key_exists($period, $steps))
return $steps[$period];
return "";
}
function get_date_span($start, $stop, $period) {
$format = get_date_format($period);
$step = get_date_step($period);
$dates = array();
while($stop >= $start) {
$dates[] = date($format, $start);
$start = strtotime($step, $start);
}
return $dates;
}
function get_current($period) {
return date(get_date_format($period));
}
$date_start = strtotime($_GET["date_start"]);
$date_stop = strtotime($_GET["date_stop"]);
$date_step = $_GET["date_step"];
$metric = $_GET["metric"];
$dates = get_date_span($date_start, $date_stop, $date_step);
$dates = array_reverse($dates);
$current = get_current($date_step);
try {
$sys = new ColumnFamily(cassandra_connect("fo"), "systems");
$agg = new ColumnFamily(cassandra_connect("metrics"), "agg");
$sys->read_consistency_level = cassandra_ConsistencyLevel::ONE;
$agg->read_consistency_level = cassandra_ConsistencyLevel::ONE;
if($_GET["load"] == "all") {
$systems = array();
$r = $sys->get_range("", "", $row_count=200, $columns=array("status"));
foreach($r as $system => $info) $systems[] = $system;
} else {
$systems = explode(",", $_GET["load"]);
}
$keys = array();
$totals = array();
foreach($systems as $system) {
$totals[$system] = array();
$keys["$system-$metric-$date_step"] = $system;
}
$result = $agg->multiget(array_keys($keys), $dates);
foreach($result as $key => $xfer) {
$system = $keys[$key];
foreach($dates as $date) {
$totals[$system][$date] = array_key_exists($date, $xfer) ? $xfer[$date] : "";
}
}
if(in_array($current, $dates) && in_array($date_step, array("monthly", "daily"))) {
$keys = array();
$skeymap = array();
if($date_step == "monthly") {
$keys["daily"] = array();
$keys["daily"]["map"] = array();
$keys["daily"]["columns"] = get_date_span(strtotime("first day of this month"), time(), "daily");
}
$keys["hourly"] = array();
$keys["hourly"]["map"] = array();
$keys["hourly"]["columns"] = get_date_span(strtotime("today"), time(), "hourly");
foreach($systems as $system) {
$skeymap[$system] = array();
foreach($keys as $key => &$instance) {
$instance["map"]["$system-$metric-$key"] = $system;
$skeymap[$system][$key] = "$system-$metric-$key";
}
}
foreach($keys as $key => &$instance) {
$instance["result"] = $agg->multiget(array_keys($instance["map"]), $instance["columns"]);
}
foreach($systems as $system) {
$total = 0;
foreach($keys as $key => &$instance) {
$skey = $skeymap[$system][$key];
$result = $instance["result"];
if(array_key_exists($skey, $result)) {
foreach($instance["columns"] as $column) {
if(array_key_exists($column, $result[$skey])) {
$total += intval($result[$skey][$column]);
}
}
}
}
$totals[$system][$current] = strval($total);
}
}
ob_start('ob_gzhandler');
echo json_encode(array("columns" => $dates, "data" => $totals));
} catch(Exception $e) {
die("exception");
}
?>