-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.php
More file actions
97 lines (70 loc) · 2.33 KB
/
index.php
File metadata and controls
97 lines (70 loc) · 2.33 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
<?php
require_once( 'config.php' );
new SBServerLoad;
class SBServerLoad{
protected $db = false;
function __construct(){
global $servers;
$this->_connect_to_db();
if( array_key_exists( $_SERVER['REMOTE_ADDR'] , $servers ) && isset( $_POST['loadavg'] ) )
$this->_record_data();
else
$this->display_data();
}
public function display_data(){
global $servers;
$data = new stdClass();
$data->graph = new stdClass();
$graph = &$data->graph;
$graph->refreshEveryNSeconds = 60;
$graph->type = 'line';
$graph->title = CHART_TITLE;
$graph->datasequences = array();
$oder_by = ( true === CHART_NATURAL_TIME_DIRECTION ) ? 'ASC' : 'DESC';
$query = $this->db->prepare( "SELECT * FROM serverload WHERE timestamp > ( NOW() - INTERVAL ? MINUTE ) ORDER BY timestamp $order_by" );
$query->execute( array( DISPLAY_IN_MINUTES ));
$servers_logs = array();
while( $row = $query->fetch( PDO::FETCH_OBJ ) ){
if( !isset( $servers_logs[ $row->ip ] ) ){
$servers_logs[ $row->ip ] = array();
$graph->datasequences[] = array(
'title' => $servers[ $row->ip ],
'datapoints' => &$servers_logs[ $row->ip ]
);
}
$load = explode( " ", $row->datavalue, 2 );
$load = $load[0] * 100;
$servers_logs[ $row->ip ][] = array(
'title' => date( 'G:i', strtotime( $row->timestamp ) ),
'value' => $load
);
}
echo json_encode( $data );
}
protected function _record_data(){
$data = array(
'ip' => $_SERVER['REMOTE_ADDR'],
'loadavg' => $_POST['loadavg']
);
$this->_insert_data( $data );
$this->_cleanup();
}
protected function _insert_data( $data ){
$query = $this->db->prepare( "INSERT INTO serverload (ip, datakey, datavalue) VALUES (?,?,?)" );
$query->execute( array( $data['ip'], 'loadavg', $data['loadavg'] ) );
}
protected function _cleanup(){
if( AUTO_CLEAN_UP ){
$query = $this->db->prepare( "DELETE FROM serverload WHERE timestamp < ( NOW() - INTERVAL ? MINUTE )" );
$query->execute( array( STORE_IN_MINUTES ) );
}
}
protected function _connect_to_db(){
try {
$this->db = new PDO( "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASS,
array( PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'' ) );
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
}
}