-
Notifications
You must be signed in to change notification settings - Fork 3
/
ygg-stats.php
160 lines (140 loc) · 5.34 KB
/
ygg-stats.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
<?php
$host = "127.0.0.1";
$port = 9001;
set_time_limit(2);
//get the latest nodelist and save to file
function updateNodeList(){
$raw_node_file = file_get_contents("http://y.yakamo.org:3000/current");
$nodelist_api_pull = json_decode($raw_node_file, true);
$replace_non_alnum = array("-", ".");
$myfile = fopen("nodelist.txt", "w+") or die("Unable to create file, please check permissions!");
foreach ($nodelist_api_pull{"yggnodes"} as $key => $value) {
if(count($value) === 3){
if (ctype_alnum(str_replace($replace_non_alnum, "", $value{2}))) {
fwrite($myfile, $key . " " . $value{2} . "\n");
}
}
}
fclose($myfile);
// check file age if over 2hrs update
function check_if_update_needed(){
if (file_exists("nodelist.txt")) {
if (time()-filemtime("nodelist.txt") > 10) {
updateNodeList();
}
}else {
updateNodeList();
}
}
// open file and create array
function parse_nodelist() {
$data = array();
$raw_nodes = explode("\n", file_get_contents("nodelist.txt"));
foreach ($raw_nodes as &$value) {
if ($value) {
$tempy = explode(" ", $value);
$data[$tempy[1]] = $tempy[0];
}
}
return $data;
}
// check to see if ipv6 is in the nodelist and return an alias
function nodelist_index($key, $nodelist) {
if (in_array($key, $nodelist)) {
echo '<div class="item"><span class="name">' .
array_search($key, $nodelist) .
'</span><span class="keylabel">' .
$key .
'</span></div>';
}else {
echo $key;
}
}
// convert bytes to something human readable
function humanFileSize($size,$unit="") {
if( (!$unit && $size >= 1<<30) || $unit == "GB")
return number_format($size/(1<<30),2)."GB";
if( (!$unit && $size >= 1<<20) || $unit == "MB")
return number_format($size/(1<<20),2)."MB";
if( (!$unit && $size >= 1<<10) || $unit == "KB")
return number_format($size/(1<<10),2)."KB";
return number_format($size)." bytes";
}
// convert seconds to human readable time
function secondsToTime($seconds) {
$dtF = new \DateTime('@0');
$dtT = new \DateTime("@$seconds");
return $dtF->diff($dtT)->format('%a days, %h:%i.%s');
}
$getPeers = '{"request": "getPeers","keepalive":true}';
$getSessions = '{"request": "getSessions","keepalive":true}';
$getSelf = '{"request":"getSelf"}';
// check if nodelist.txt exists and if it needs updated or created
check_if_update_needed();
$nodelist_array = parse_nodelist();
// open socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_connect($socket, $host, $port) or die("Could not connect toserver\n");
// getPeers request
socket_write($socket, $getPeers, strlen($getPeers)) or die("Could not send data to server\n");
$gpeers = socket_read ($socket, 20024) or die("Could not read server response\n");
// getSessions
socket_write($socket, $getSessions, strlen($getSessions)) or die("Could not send data to server\n");
$gsessions = socket_read ($socket, 20024) or die("Could not read server response\n");
// getSelf
socket_write($socket, $getSelf, strlen($getSelf)) or die("Could not send data to server\n");
$gself = socket_read ($socket, 2024) or die("Could not read server response\n");
// make sure connection is closed
socket_close($socket);
// convert to arrays
$getSelf_json_array = json_decode($gself, true);
$getSessions_json_array = json_decode($gsessions, true);
$getPeers_json_array = json_decode($gpeers, true);
?>
<!-- body of starts here -->
<!DOCTYPE html>
<html>
<head>
<title><?php echo key($getSelf_json_array{"response"}{"self"}); ?></title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<div id="header">
<div id="title"><?php echo nodelist_index(key($getSelf_json_array{"response"}{"self"}), $nodelist_array); ?></div>
</div>
<div id="wrapper">
<span class="statcount">
<?php
echo "Connected Peers: " . count($getPeers_json_array{"response"}{"peers"}) . " - ";
echo "Current Sessions: " . count($getSessions_json_array{"response"}{"sessions"});
?>
</span>
<h3>Connected Peers</h3>
<?php
// getPeers display and oragnise data pretty here
foreach ($getPeers_json_array{"response"}{"peers"} as $key => $value) {
if ($value{"port"}) {
echo '<div class="peer">' . nodelist_index($key, $nodelist_array) . '</div>';
echo '<div class="data"><div class="col1">' . secondsToTime((int)$value{"uptime"}) . '</div>';
echo '<div class="col2">Rx: ' . humanFileSize($value{"bytes_recvd"}) . '</div>';
echo '<div class="col2">Tx: ' . humanFileSize($value{"bytes_sent"}). '</div>';
echo '<div class="col2">Port: ' . $value{"port"} . '</div>';
echo '<div class="clear"></div></div>';
}
}
echo '<h3>Current Sessions</h3>';
// getSessions display and oragnise data pretty here
foreach ($getSessions_json_array{"response"}{"sessions"} as $key => $value) {
echo '<div class="peer">' . nodelist_index($key, $nodelist_array) . '</div>';
echo '<div class="data">';
echo '<div class="col2">Rx: ' . humanFileSize($value{"bytes_recvd"}) . '</div>';
echo '<div class="col2">Tx: ' . humanFileSize($value{"bytes_sent"}) . '</div>';
echo '<div class="col2">Coords: ' . $value{"coords"} . '</div>';
echo '<div class="clear"></div></div>';
}
?>
<br />
<a href="https://github.com/yakamok/yggdrasil-stats">ygg-stats</a>
</div>
</body>
</html>