Robert Marshall il y a 9 ans
Parent
commit
3c96daaff4

+ 19 - 0
Controller/Navigation/StatusNav.php

@@ -0,0 +1,19 @@
+<?php
+class StatusNav implements INavigationController{
+	private $_uri;
+	
+	public function __construct() {
+		if (Session::IsUserLoggedIn() && Session::GetLoggedInUser()->HasAccess("status/index"))
+			$this->_uri=new URI("Server Status","/status","/images/status.svg");
+		else
+			$this->_uri=null;
+	}
+	
+	public function GetItems() {
+		
+	}
+
+	public function GetURI() {
+		return $this->_uri;
+	}
+}

+ 48 - 0
Controller/Status.php

@@ -0,0 +1,48 @@
+<?php
+
+ApplicationSettings::RegisterDefaultSetting("status", "disks", "/");
+
+class Status extends Controller {
+
+	public function Index() {
+		$vars=array();
+
+		$uptime=exec("uptime");
+		#$uptime="17:16:18 up 22 days,  9:39,  1 user,  load average: 2.10, 2.18, 2.33";
+		$matches=array();
+		preg_match("/(\d+) days,\s*(\d*):(\d*)/", $uptime, $matches);
+		$vars['uptime']=[
+			"days"=>$matches[1],
+			"hours"=>$matches[2],
+			"minutes"=>$matches[3]
+		];
+
+		if (function_exists("sys_getloadavg"))
+			$vars["load"]=sys_getloadavg();
+		else
+			$vars['load']=array(4, 4, 4);
+
+		$data=explode("\n", trim(file_get_contents("/proc/meminfo")));
+		$memInfo=array();
+		foreach ($data as $line) {
+			list($key, $val)=explode(":", $line);
+			$memInfo[$key]=explode(" ",trim($val))[0];
+		}
+		
+		$vars['memFree']=$memInfo['MemFree'];
+		$vars['memTotal']=$memInfo['MemTotal'];
+		$vars['memUsage']=(($vars['memTotal']-$vars['memFree'])/$vars['memTotal'])*100;
+		
+		$cpuTemp=(int)file_get_contents("/sys/class/thermal/thermal_zone0/temp");
+		$vars['cpuTemp']=$cpuTemp/1000;
+
+		$vars['disks']=array();
+		$disks=explode(",", ApplicationSettings::GetSetting("status", "disks"));
+		foreach ($disks as $disk) {
+			$vars['disks'][]=new DiskInfo($disk);
+		}
+
+		return new View("Status/index.view", $vars);
+	}
+
+}

+ 1 - 0
DB Scripts/8/insert_permissions_data.sql

@@ -0,0 +1 @@
+INSERT INTO permissions (permission_value, permission_default_grant) VALUES ('status/index', 0);

+ 42 - 0
Model/DiskInfo.php

@@ -0,0 +1,42 @@
+<?php
+
+class DiskInfo {
+	private $_disk;
+	private static $Postfix=["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
+
+	public function __construct($disk) {
+		$this->_disk=$disk;
+	}
+
+	public function __get($name) {
+		return $this->$name();
+	}
+
+	private function HumanSize($bytes) {
+		$index=0;
+		while ($bytes>=1024) {
+			$bytes/=1024;
+			$index++;
+		}
+		return round($bytes, 2)." ".self::$Postfix[$index];
+	}
+
+	private function FreeSpace() {
+		return $this->HumanSize(disk_free_space($this->_disk));
+	}
+
+	private function TotalSpace() {
+		return $this->HumanSize(disk_total_space($this->_disk));
+	}
+
+	private function PercentageUsed(){
+		$free=disk_free_space($this->_disk);
+		$total=disk_total_space($this->_disk);
+		$remaining=($total-$free)/$total;
+		return round($remaining*100,2);
+	}
+	
+	private function Disk(){
+		return $this->_disk;
+	}
+}

+ 18 - 0
View/Status/index.view

@@ -0,0 +1,18 @@
+@Body{
+	<h2>Server Status</h2>
+	Uptime: <?=$uptime['days']?> days, <?=$uptime['hours']?> hours and <?=$uptime['minutes']?> minutes
+	<h3>Load</h3>
+	<table class="alternatingRows">
+		<tr><td>1</td><td><?= $load[0]*100 ?>% (<?= ($load[0]/4)*100 ?>% core avg)</td></tr>
+		<tr><td>5</td><td><?= $load[1]*100 ?>% (<?= ($load[1]/4)*100 ?>% core avg)</td></tr>
+		<tr><td>15</td><td><?= $load[2]*100 ?>% (<?= ($load[2]/4)*100 ?>% core avg)</td></tr>
+		<tr><td>CPU Temp</td><td><?=round($cpuTemp)?>&deg;C</td></tr>
+		<tr><td>RAM</td><td><?=round($memFree/1024,2)?>MB free of <?=round($memTotal/1024,2)?>MB (<?=round($memUsage)?>% used)<div class="percentageBar"><div style="width:<?=$memUsage?>%"></div></td></tr>
+	</table>
+	<h3>Disc Space</h3>
+	<table class="alternatingRows">
+		<?php foreach ($disks as $disk){?>
+			<tr><td><?=$disk->Disk?></td><td><?=$disk->FreeSpace?> of <?=$disk->TotalSpace?> remaining (<?=$disk->PercentageUsed?>% used)<div class="percentageBar"><div style="width:<?=$disk->PercentageUsed?>%"></div></td></tr>
+		<?php } ?>
+	</table>
+}@

+ 9 - 0
css/style.css

@@ -302,3 +302,12 @@ td.number,
 th.number {
   text-align: right;
 }
+div.percentageBar {
+  width: 100%;
+  height: 20px;
+  background: #4caf50;
+}
+div.percentageBar > div {
+  background: #d32f2f;
+  height: 100%;
+}

+ 16 - 0
images/status.svg

@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+<svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
+	 width="24px" height="24px" viewBox="0 0 24 24" enable-background="new 0 0 24 24" xml:space="preserve">
+<rect x="4" y="17" fill="#BA68C8" width="10" height="2"/>
+<rect x="4" y="9" fill="#4DB6AC" width="16" height="2"/>
+<rect x="4" y="13" fill="#DCE775" width="16" height="2"/>
+<rect x="4" y="5" fill="#4FC3F7" width="16" height="2"/>
+<path fill="none" d="M0,0v24h24V0H0z M22.49,19.42H13.97V19H14v-2h-0.03v-2H20v-2h-6.03v-2H20V9h-6.03V7H20V5h-6.03V3.28h8.521
+	V19.42z"/>
+<rect x="13.97" y="17" opacity="0.2" fill="#546E7A" width="0.03" height="2"/>
+<rect x="13.97" y="5" opacity="0.2" fill="#546E7A" width="6.03" height="2"/>
+<rect x="13.97" y="9" opacity="0.2" fill="#546E7A" width="6.03" height="2"/>
+<rect x="13.97" y="13" opacity="0.2" fill="#546E7A" width="6.03" height="2"/>
+</svg>

+ 11 - 0
less/style.less

@@ -360,4 +360,15 @@ td, th{
 	&.number{
 		text-align: right;
 	}
+}
+
+div.percentageBar{
+	width:100%;
+	height:20px;
+	background:@Green-500;
+	
+	>div{
+		background:@Red-700;
+		height:100%;
+	}
 }

+ 5 - 2
settings.ini

@@ -11,7 +11,7 @@ database=robware-test
 
 [navigation]
 mode=include
-pages=HomeNav,BlogNav,ProjectsNav,GalleryNav,DesktopNav,WeightNav,MemberNav
+pages=HomeNav,BlogNav,ProjectsNav,GalleryNav,DesktopNav,WeightNav,StatusNav,MemberNav
 
 [blog]
 posts_per_page=10
@@ -24,4 +24,7 @@ IBlogPostRepository=BlogPostRepository
 IWeightReadingRepository=WeightReadingRepository
 IAlbumRepository=AlbumRepository
 IImageRepository=ImageRepository
-IUserSettingsRepository=UserSettingsRepository
+IUserSettingsRepository=UserSettingsRepository
+
+[status]
+disks=C:\,G:\,S:\