Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/archive/status.php
blob: 5946dcc10b8f284bb17e1e0c56abe4575f4105ee (plain)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<!DOCTYPE html>
<html lang="ru">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<title>Техническая информация | Веб-сайт самохостера Lotigara</title>
		<link href="/css/main.css" rel="stylesheet" />
		<link href="/css/theme.css" rel="stylesheet" />
		<link href="/css/third-party.css" rel="stylesheet" />
		<link href="/css/responsivity.css" rel="stylesheet" />

		<style>
			@media (max-width: 1024px) {
				tr.services_table_item td:nth-of-type(1):before { content: "Название"; }
				tr.services_table_item td:nth-of-type(2):before { content: "Порт"; }
				tr.services_table_item td:nth-of-type(3):before { content: "Состояние"; }

				tr.status_table_item td:nth-of-type(1):before { content: "Средняя загрузка"; }
				tr.status_table_item td:nth-of-type(2):before { content: "Время работы"; }
				tr.status_table_item td:nth-of-type(3):before { content: "Свободно ОЗУ"; }
			}
		</style>
	</head>
	<body>
		<input type="checkbox" id="theme-switch">
		<div id="page"><div class="inner_page">
			<div class="page_header floating_element"><h1 class="floating_element">Техническая информация</h1></div>
			<div class="floating_element">
				<h4 style="display:inline"><a style="color:white" href="/">главная|main page</a></h4>
				<p style="display:inline">состояние|status</p>
				<h4 style="display:inline"><a style="color:white" href="/blog">журнал|journal</a></h4>
				<h4 style="display:inline"><a style="color:white" href="/files">файлы|files</a></h4>
				<h4 style="display:inline"><a style="color:white" href="https://git.lotigara.ru">программы|software</a></h4>
				<label id="switch-label" for="theme-switch"></label>
			</div>
			<div class="table_of_contents floating_element">
				<details>
					<summary class="section_header section_header_last">
						Содержание
					</summary>
					<div class="table_of_contents_item floating_element">
						<a href="#net-daemons">Состояние сетевых демонов</a>
					<div class="table_of_contents_item floating_element">
						<a href="#server-info">Информация о сервере</a>
					</div>
					<div class="table_of_contents_item floating_element">
						<a href="#license">Лицензия</a>
					</div>
				</details>
			</div>
			<div class="content_section floating_element">
<?php
/*

 *
 * @author      Trường An Phạm Nguyễn, Ivan Davydov
 * @copyright   2019, 2024, The authors
 * @license     GNU AFFERO GENERAL PUBLIC LICENSE
 *        http://www.gnu.org/licenses/agpl-3.0.html
 *
 * Jul 27, 2013

Original author:
*       Disclaimer Notice(s)                                                          
*       ex: This code is freely given to you and given "AS IS", SO if it damages      
*       your computer, formats your HDs, or burns your house I am not the one to
*       blame.                                                                     
*       Moreover, don't forget to include my copyright notices and name.               
*   +------------------------------------------------------------------------------+
*       Author(s): Crooty.co.uk (Adam C)                                    
*   +------------------------------------------------------------------------------+

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/  

$data = "";
$data .= '<div class="section_header"><div id="server-info"></div>Состояние сетевых демонов</div><div class="content_section_text">';


//configure script
$timeout = "1";

//set service checks
/* 
The script will open a socket to the following service to test for connection.
Does not test the fucntionality, just the ability to connect
Each service can have a name, port and the Unix domain it run on (default to localhost)
*/
$services = array();


$services[] = array("port" => "80",   "service" => "Nginx",                     "ip" => "") ;
$services[] = array("port" => "5222", "service" => "Prosody",                   "ip" => "") ;
$services[] = array("port" => "5281", "service" => "Веб-сервер Prosody",        "ip" => "") ;
$services[] = array("port" => "25",   "service" => "Postfix",			"ip" => "") ;
$services[] = array("port" => "143",  "service" => "Dovecot",             	"ip" => "") ;
$services[] = array("port" => "1323", "service" => "Alps",             	        "ip" => "") ;
$services[] = array("port" => "5232", "service" => "Radicale",                  "ip" => "") ;
$services[] = array("port" => "80",   "service" => "Yandex",                    "ip" => "ya.ru") ;


//begin table for status
$data .= "<table class='services_table'><thead><tr><th>Название</th><th>Порт</th><th>Состояние</th></tr></thead><tbody>";
foreach ($services  as $service) {
	if($service['ip']==""){
	   $service['ip'] = "localhost";
	}
	$data .= "<tr class='services_table_item'><td>" . $service['service'] . "</td><td>". $service['port'];

	$fp = @fsockopen($service['ip'], $service['port'], $errno, $errstr, $timeout);
	if (!$fp) {
		$data .= "</td><td>Не работает </td></tr>";
	  //fclose($fp);
	} else {
		$data .= "</td><td>Работает</td></tr>";
		fclose($fp);
	}

}  
//close table
$data .= "</tbody></table>";
$data .= '</div>';
echo $data;


/* =====================================================================
//
// ////////////////// SERVER INFORMATION  /////////////////////////////////
//
//
* =======================================================================/*/

$data1 = "";
$data1 .= '<div class="section_header"><div id="server-info"></div>Информация о сервере</div><div class="content_section_text">';

$data1 .= "<table class='status_table'><thead><tr><th>Средняя загрузка</th><th>Время работы</th><th>Свободно ОЗУ</th></tr></thead><tbody>";
// $data1 .= "<div class='table-responsive'><table  class='table table-sm mb-0'>";

//GET SERVER LOADS
$loadresult = @exec('uptime');  
preg_match("/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/",$loadresult,$avgs);


//GET SERVER UPTIME
$uptime = explode(' up ', $loadresult);
$uptime = explode(',', $uptime[1]);
$uptime = $uptime[0].', '.$uptime[1];

//Get the disk space
function getSymbolByQuantity($bytes) {
	$symbol = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB');
	$exp = floor(log($bytes)/log(1024));
	
	return sprintf('%.2f'.$symbol[$exp].'', ($bytes/pow(1024, floor($exp))));
}
function percent_to_color($p){
	if($p < 30) return 'success';
	if($p < 45) return 'info';
	if($p < 60) return 'primary';
	if($p < 75) return 'warning';
	return 'danger';
}
function format_storage_info($disk_space, $disk_free, $disk_name){
	$str = "";
	$disk_free_precent = 100 - round($disk_free*1.0 / $disk_space*100, 2);
		$str .= '<div class="col p-0 d-inline-flex">';
		$str .= "<span class='mr-2'>" . badge($disk_name,'secondary') .' '. getSymbolByQuantity($disk_free) . '/'. getSymbolByQuantity($disk_space) ."</span>";
		$str .= '
<div class="progress flex-grow-1 align-self-center">
  <div class="progress-bar progress-bar-striped progress-bar-animated ';
		$str .= 'bg-' . percent_to_color($disk_free_precent) .'
  " role="progressbar" style="width: '.$disk_free_precent.'%;" aria-valuenow="'.$disk_free_precent.'" aria-valuemin="0" aria-valuemax="100">'.$disk_free_precent.'%</div>
</div>
</div>		';

	return $str;

}

function get_disk_free_status($disks){
	$str="";
	$max = 5;
	foreach($disks as $disk){
		if(strlen($disk["name"]) > $max) 
			$max = strlen($disk["name"]);
	}
	
	foreach($disks as $disk){
		$disk_space = disk_total_space($disk["path"]);
		$disk_free = disk_free_space($disk["path"]);

		$str .= format_storage_info($disk_space, $disk_free, $disk['name']);

	}
	return $str;
}
function badge($str, $type){
	return "<span class='badge badge-" . $type . " ' >$str</span>";
}

//Get ram usage
$total_mem = preg_split('/ +/', @exec('grep MemTotal /proc/meminfo'));
$total_mem = $total_mem[1];
$free_mem = preg_split('/ +/', @exec('grep MemFree /proc/meminfo'));
$cache_mem = preg_split('/ +/', @exec('grep ^Cached /proc/meminfo'));

$free_mem = $free_mem[1] + $cache_mem[1];


//Get top mem usage
$tom_mem_arr = array();
$top_cpu_use = array();

//-- The number of processes to display in Top RAM user
$i = 5;


/* ps command:
-e to display process from all user
-k to specify sorting order: - is desc order follow by column name
-o to specify output format, it's a list of column name. = suppress the display of column name
head to get only the first few lines 
*/
/*exec("ps -e k-rss -o rss,args | head -n $i", $tom_mem_arr, $status);
exec("ps -e k-pcpu -o pcpu,args | head -n $i", $top_cpu_use, $status);


$top_mem = implode('<br/>', $tom_mem_arr );
$top_mem = "<pre class='mb-0 '><code>" . $top_mem . "</code></pre>";

$top_cpu = implode('<br/>', $top_cpu_use );
$top_cpu = "<pre class='mb-0 '><code>" . $top_cpu. "</code></pre>";*/

$data1 .= "<tr class='status_table_item'><td><b>". $avgs[1]. ' ' .$avgs[2] . ' ' . $avgs[3] . " </b></td>\n";
$data1 .= "<td>$uptime</td>";


$disks = array();

/*
* The disks array list all mountpoint you wan to check freespace
* Display name and path to the moutpoint have to be provide, you can 
*/
$disks[] = array("name" => "local" , "path" => getcwd()) ;
// $disks[] = array("name" => "Your disk name" , "path" => '/mount/point/to/that/disk') ;


/*$data1 .= "<tr><td>Disk free        </td><td>" . get_disk_free_status($disks) . "</td></tr>";*/

$data1 .= "<td>". format_storage_info($total_mem *1024, $free_mem *1024, '') ."</td>";
/*$data1 .= "<tr><td>Top RAM user    </td><td><small>$top_mem</small></td></tr>";
$data1 .= "<tr><td>Top CPU user    </td><td><small>$top_cpu</small></td></tr>";*/
$data1 .= "<hr>";

$data1 .= "</tr>";
$data1 .= "</tbody>";
$data1 .= "</table>";
$data1 .= '</div>';
echo $data1;  

/* =============================================================================
*
* DISPLAY BANDWIDTH STATISTIC, REQUIRE VNSTAT INSTALLED AND PROPERLY CONFIGURED.
*
* ===============================================================================s
*/


/*if (!isset($_GET['showtraffic']) || $_GET['showtraffic'] ==  false) die();

$data2 = "";
$data2 .=  '
<div class="card mb-2">
  <h4 class="card-header text-center">
    vnstat Network traffic
  </h4>
  <div class="card-body text-center">
';


$data2 .="<span class=' d-block'><pre class='d-inline-block text-left'><small>";
$traffic_arr = array();
exec('vnstat -' . escapeshellarg( $_GET['showtraffic'] ), $traffic_arr, $status);

///for testing
$traffic = "
enp0s20  /  monthly

month        rx      |     tx      |    total    |   avg. rate
------------------------+-------------+-------------+---------------
Sep '18     36.60 GiB |    7.04 GiB |   43.64 GiB |  144.62 kbit/s
Oct '18    400.69 GiB |    1.19 TiB |    1.58 TiB |    5.19 Mbit/s
Nov '18    393.52 GiB |    2.19 TiB |    2.57 TiB |    8.72 Mbit/s
Dec '18    507.28 GiB |    2.05 TiB |    2.55 TiB |    8.37 Mbit/s
Jan '19    269.01 GiB |    1.39 TiB |    1.65 TiB |    7.51 Mbit/s
------------------------+-------------+-------------+---------------
estimated    371.92 GiB |    1.92 TiB |    2.29 TiB |
";
/// for real
$traffic = implode("\n", $traffic_arr);

$data2 .="$traffic</small></pre></span>";

echo $data2;*/
?>
<div class="section_header section_header_last">
	<div id="license"></div>
	См. лицензию и исходный код в <a style="color: white;" href="/files/status.php.txt">этом</a> файле
</div>
</div></div></body></html>