├── README.md ├── cleanDB.php ├── config.php ├── cpu.php ├── db.php ├── funs.php ├── index.php ├── install.sh ├── measuretemp.php ├── style.css └── temp.php /README.md: -------------------------------------------------------------------------------- 1 | Raspberry-Pi-Heartbeat 2 | ====================== 3 | 4 | ##Status webpage for Raspberry Pi## 5 | 6 | ![Raspberry Pi Heartbeat](http://i.imgur.com/nEqeHF6.png "Latest version screenshot:") 7 | 8 | ###Prerequisites### 9 | You will have to check a few things for this page to show up correctly. You need to have: 10 | 11 | * lsb-release (for non Raspbian distributions) - so the automated installer script can find your distribution name. 12 | * Webserver (apache \ lighttpd \ ..) with PHP 13 | 14 | 15 | #####Optional (./install.sh will install them for you): 16 | * Sqlite for PHP `$ sudo apt-get install php5-sqlite` 17 | * PHP Safe Mode = off 18 | * vstat installed `$ sudo apt-get install vnstat` 19 | 20 | First 3 are more or less general and common. VNSTAT is additional app which collects traffic information. You might fiddle around with it's settings after the installation if you want. Main requirement is that it does give an output with this command `$ vnstat --dumpdb`. 21 | 22 | ###Contents of the package & Installation### 23 | 24 | As for now: 8-9 files 25 | 26 | * config.php - Where you can configure all the settings (folder path, verbosity and future options) 27 | * index.php - Home page which shows the graphs and info. 28 | * style.css - Styles for the fashion! 29 | * rpiTemp.db - SQLite db file. With some test data. 30 | * measuretemp.php - php script for cron job. 31 | * cleanDB.php - script to clean the DB from sampling data and forbid future accidental deletions using a lock file. 32 | * db.lck - lock file used by cleanDB.php to flag the clean operation as done and forbid future database cleanups. If you do want to reset the database, then delete db.lck first. 33 | * funs.php - extras for Ajax and live measurements 34 | 35 | **To set it up:** 36 | 37 | 1. Download a zip from [this page] (http://geekbeard.github.io/Raspberry-Pi-Heartbeat/) and unzip to a desired web folder (i.e. /var/www/rpih/) 38 | 2. Open config.php and update `$config["root_dir"]` the with directory you put it in. For example, if you put it into /var/www/rpih/: `$config["root_dir"]="/var/www/rpih/"` 39 | 3. Check and install if needed the prerequisites (see above) OR 40 | 4. Run `$ sudo ./install.sh` - this will try to install all missing prerequisites, initialize the database with the first temperature sample and update the crontab with the periodical temperature sampling. Default is a sample every 5 minutes. This can later be changed in `$ sudo crontab -e` 41 | 42 | You can also manually add the cron job to your system/download the 43 | packages/clean the database. `$ sudo crontab -e` and add a line at the end of the file: 44 | `*/5 * * * * php /path/measuretemp.php` - /path/ is the place where you've 45 | unziped the package! This will run measuretemp.php every 5 minutes. If you want 46 | it to happen less often, change 5 to a higher number of minutes (*/10 for every 47 | 10 minutes, */35 for every 35 minutes and so on). 48 | 49 | The database can be cleaned using cleanDB.php, which will also produce a lock 50 | file to avoid future accidental deletions. Delete the lock file (`db.lck`) first if you 51 | really want to clean the database. 52 | 53 | **You are done!** 54 | 55 | You've unzipped the package, created a scheduled task for Raspbian to run the php script every N minutes and you have cleaned the database from test data and the script created a fail safe flag (file: `db.lck`) which will prevent anyone from cleaning db again! 56 | 57 | Access the webpage with the web browser and start collecting statistics!:) 58 | 59 | And of course come back for updated versions! 60 | 61 | ###Authors and Contributors### 62 | First version by: @beard 63 | 64 | ###Support or Contact### 65 | If you have any questions \ suggestions \ feedbacks: geekbearddev@gmail.com - we hope you send us a note!:) 66 | 67 | Please feel free to fork and improve! 68 | -------------------------------------------------------------------------------- /cleanDB.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | "; 24 | DB::exec("CREATE TABLE 'rpi_temp' ('id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 'temp' REAL, 'time' TEXT, 'cpu' REAL,'rtemp' REAL,'hum' REAL)"); 25 | 26 | if($verbose) 27 | echo "DB created!
"; 28 | 29 | Stats::measureTemp(); 30 | 31 | if($verbose) 32 | echo "First values inserted! Good luck:)
"; 33 | 34 | file_put_contents($filename, $s); 35 | 36 | } 37 | ?> 38 | 39 | -------------------------------------------------------------------------------- /config.php: -------------------------------------------------------------------------------- 1 | 17 | -------------------------------------------------------------------------------- /cpu.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /db.php: -------------------------------------------------------------------------------- 1 | 2 | query($query); 20 | } 21 | public static function exec($query) { 22 | DB::init(); 23 | return DB::$db->exec($query); 24 | } 25 | } 26 | 27 | ?> -------------------------------------------------------------------------------- /funs.php: -------------------------------------------------------------------------------- 1 | fetchArray()) { 11 | $id = $row['id']; 12 | $cpu=$row['cpu']; 13 | $dtime=substr($row['time'], -8, 5); 14 | $cpu=$row['cpu']; 15 | 16 | $buf .= "['$dtime',$cpu],"; 17 | 18 | } 19 | return $buf ; 20 | } 21 | 22 | public static function cpuLoad() { 23 | //CPU Usage 24 | $output1 = null; 25 | $output2 = null; 26 | //First sample 27 | exec("cat /proc/stat", $output1); 28 | //Sleep before second sample 29 | sleep(1); 30 | //Second sample 31 | exec("cat /proc/stat", $output2); 32 | $cpuload = 0; 33 | for ($i=0; $i < 1; $i++) 34 | { 35 | //First row 36 | $cpu_stat_1 = explode(" ", $output1[$i+1]); 37 | $cpu_stat_2 = explode(" ", $output2[$i+1]); 38 | //Init arrays 39 | $info1 = array("user"=>$cpu_stat_1[1], "nice"=>$cpu_stat_1[2], "system"=>$cpu_stat_1[3], "idle"=>$cpu_stat_1[4]); 40 | $info2 = array("user"=>$cpu_stat_2[1], "nice"=>$cpu_stat_2[2], "system"=>$cpu_stat_2[3], "idle"=>$cpu_stat_2[4]); 41 | $idlesum = $info2["idle"] - $info1["idle"] + $info2["system"] - $info1["system"]; 42 | $sum1 = array_sum($info1); 43 | $sum2 = array_sum($info2); 44 | //Calculate the cpu usage as a percent 45 | $load = (1 - ($idlesum / ($sum2 - $sum1))) * 100; 46 | $cpuload += $load; 47 | } 48 | $cpuload = round($cpuload, 1); //One decimal place 49 | return $cpuload ; 50 | } 51 | 52 | public static function temperatureURL() { 53 | include "config.php" ; 54 | return "\"".$config["temperatureURL"]."\"" ; 55 | } 56 | 57 | public static function cpuURL() { 58 | include "config.php" ; 59 | return "\"".$config["cpuURL"]."\"" ; 60 | } 61 | 62 | public static function temperature() { 63 | $temp = round(exec("cat /sys/class/thermal/thermal_zone0/temp ") / 1000, 1) ; 64 | return $temp ; 65 | } 66 | 67 | public static function temperatures() { 68 | $buf = ""; 69 | $results = DB::query("SELECT * FROM rpi_temp ORDER BY id DESC LIMIT 0,288"); 70 | while ($row = $results->fetchArray()) { 71 | $id = $row['id']; 72 | $temp=$row['temp']; 73 | $rtemp=$row['rtemp']; 74 | $dtime=substr($row['time'], -8, 5); 75 | $cpu=$row['cpu']; 76 | 77 | $buf .= "['$dtime',$temp],"; //fix here!!! 78 | 79 | } 80 | 81 | return $buf ; 82 | } 83 | 84 | public static function measureTemp() { 85 | $temp=round(exec("cat /sys/class/thermal/thermal_zone0/temp ") / 1000, 1); 86 | $dtime=date("Y-m-d H:i:s", strtotime ("+0 hour")); 87 | 88 | //dht stuff 89 | /* $fh = fopen('dht11.v','r'); 90 | while ($line = fgets($fh)) { 91 | $dhtout = $line; 92 | } 93 | fclose($fh);exs 94 | //$dhtout = exec("cat /var/www/temp/dht11.v "); 95 | $dhtvalues = explode(";",$dhtout); 96 | $rtemp = $dhtvalues[1]; 97 | $hum = $dhtvalues[0]; 98 | 99 | */ 100 | //end dht stuff 101 | 102 | 103 | 104 | //CPU Usage 105 | $output1 = null; 106 | $output2 = null; 107 | //First sample 108 | exec("cat /proc/stat", $output1); 109 | //Sleep before second sample 110 | sleep(1); 111 | //Second sample 112 | exec("cat /proc/stat", $output2); 113 | $cpuload = 0; 114 | for ($i=0; $i < 1; $i++) 115 | { 116 | //First row 117 | $cpu_stat_1 = explode(" ", $output1[$i+1]); 118 | $cpu_stat_2 = explode(" ", $output2[$i+1]); 119 | //Init arrays 120 | $info1 = array("user"=>$cpu_stat_1[1], "nice"=>$cpu_stat_1[2], "system"=>$cpu_stat_1[3], "idle"=>$cpu_stat_1[4]); 121 | $info2 = array("user"=>$cpu_stat_2[1], "nice"=>$cpu_stat_2[2], "system"=>$cpu_stat_2[3], "idle"=>$cpu_stat_2[4]); 122 | $idlesum = $info2["idle"] - $info1["idle"] + $info2["system"] - $info1["system"]; 123 | $sum1 = array_sum($info1); 124 | $sum2 = array_sum($info2); 125 | //Calculate the cpu usage as a percent 126 | $load = (1 - ($idlesum / ($sum2 - $sum1))) * 100; 127 | $cpuload += $load; 128 | } 129 | $cpuload = round($cpuload, 1); //One decimal place 130 | $rtemp = 0; 131 | $hum = 0; 132 | 133 | $q="INSERT INTO rpi_temp(temp,time,cpu,rtemp,hum) VALUES('$temp','$dtime','$cpuload','$rtemp','$hum')"; 134 | DB::exec($q); 135 | } 136 | 137 | 138 | public static function network() { 139 | $network = []; 140 | //traffic 141 | $traffic=null; 142 | exec("vnstat --dumpdb", $traffic); 143 | $network['alltrxL']=explode(";",$traffic[6]); 144 | $network['allttxL']=explode(";",$traffic[7]); 145 | 146 | $network['alltrx'] = $network['alltrxL'][1]; 147 | $network['allttx'] = $network['allttxL'][1]; 148 | //$alltrxtx = $alltrx+$allttx; 149 | 150 | $network['day0'] = explode(";",$traffic[13]); 151 | $network['day0rx'] = $network['day0'][3]; 152 | $network['day0tx'] = $network['day0'][4]; 153 | 154 | $network['day1'] = explode(";",$traffic[14]); 155 | $network['day1rx'] = $network['day1'][3]; 156 | $network['day1tx'] = $network['day1'][4]; 157 | 158 | $network['day2'] = explode(";",$traffic[15]); 159 | $network['day2rx'] = $network['day2'][3]; 160 | $network['day2tx'] = $network['day2'][4]; 161 | 162 | $network['mon0'] = explode(";",$traffic[43]); 163 | $network['mon0rx'] = $network['mon0'][3]; 164 | $network['mon0tx'] = $network['mon0'][4]; 165 | 166 | $network['mon1'] = explode(";",$traffic[44]); 167 | $network['mon1rx'] = $network['mon1'][3]; 168 | $network['mon1tx'] = $network['mon1'][4]; 169 | 170 | //var_dump($network); 171 | return $network ; 172 | } 173 | } 174 | 175 | 176 | ?> 177 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 10 | 11 | 12 | Raspberry Pi - Status 13 | 14 | 15 | 16 | 32 | 48 | 89 | 131 | 155 | 175 | 176 | 177 | 180 |
181 |
182 |
183 |
184 |
185 | 186 |
187 | 188 |
189 |
190 |
191 |
192 |
193 | 194 | 195 | 196 | -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | #Try to install lsb_release to get distro information 4 | #TODO add support for rasbian uname? 5 | #y: raspbian doesnt have lsb_release and can't be installed - any other way to get the right distro? 6 | #DISTRO=$(lsb_release -sd) 7 | DISTRO=$(uname -n) 8 | 9 | echo distro:$DISTRO 10 | 11 | PWD=$(pwd) 12 | echo pwd:$PWD 13 | 14 | #Add measure temp to crontab 15 | update_crontab() { 16 | sampling_interval="5" 17 | tmp_cronf="tmp_cronf" 18 | crontab -l > $tmp_cronf 19 | echo "*/$sampling_interval * * * * php $PWD/measuretemp.php" >> $tmp_cronf 20 | crontab $tmp_cronf 21 | echo "Crontab updated: Sampling temperature every $sampling_interval minutes." 22 | } 23 | 24 | #Install dependencies in Ubuntu if required 25 | install_ubuntu() { 26 | #TODO 27 | sqliteinst=$(dpkg -l | grep php5-sqlite) 28 | vnstatinst=$(dpkg -l | grep vnstat) 29 | vnstatshort=${vnstatinst:0:2} 30 | sqlshort=${sqliteinst:0:2} 31 | if [ "$sqlshort" = "ii" ]; then 32 | echo "Sqlite is installed." 33 | 34 | else 35 | echo "Need to install Sqlite." 36 | echo "Installing Sqlite for PHP" 37 | apt-get update 38 | apt-get -y install php5-sqlite 39 | exit 1 40 | fi 41 | if [ "$vnstatshort" = "ii" ]; then 42 | echo "vnstat is installed." 43 | 44 | else 45 | echo "Need to install vnstat." 46 | echo "Installing vnstat for PHP" 47 | apt-get update 48 | apt-get -y install vnstat 49 | exit 1 50 | fi 51 | } 52 | 53 | #Install dependencies in Arch Linux if required 54 | install_arch() { 55 | packages=" vnstat lsb-release php-sqlite" 56 | echo "Installing dependencies ($packages) for Arch Linux..." 57 | pacman -Syy 58 | pacman -S $packages 59 | } 60 | 61 | #TODO ADD OTHER DISTROS 62 | if [ "$DISTRO" = "Arch Linux" ]; then 63 | install_arch 64 | update_crontab 65 | php cleanDB.php 66 | exit 1 67 | elif [ "$DISTRO" = "raspberrypi" ]; then 68 | install_ubuntu 69 | update_crontab 70 | php cleanDB.php 71 | exit 1 72 | else 73 | echo "Your distribuition '$DISTRO' is not supported by this installer (yet)." 74 | exit 1 75 | fi 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /measuretemp.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /style.css: -------------------------------------------------------------------------------- 1 | @charset "utf-8"; 2 | /* CSS Document */ 3 | /* 4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 | 12 |
13 |
14 |
15 |
16 | */ 17 | 18 | #gencontainer { 19 | width: 900px; 20 | margin: 0 auto; 21 | } 22 | 23 | #topmenu { 24 | 25 | } 26 | 27 | #temp { 28 | width=100%; 29 | overflow:hidden; 30 | display:inline-block; 31 | } 32 | 33 | #chart_nowmeter { 34 | position:relative; 35 | float:left; 36 | margin-left:100px; 37 | width:256px; 38 | height:256px; 39 | display:inline-block; 40 | } 41 | 42 | #chart_cpunowmeter { 43 | position:relative; 44 | float:left; 45 | width:256px; 46 | height:256px; 47 | display:inline-block; 48 | } 49 | 50 | #traffic_div{ 51 | // position:relative; 52 | width:30%; 53 | height: 92px; 54 | } 55 | 56 | #traffic{ 57 | height:250px; 58 | position: relative; 59 | float:left; 60 | } 61 | 62 | #chart_temphistory{ 63 | 64 | 65 | } 66 | 67 | #chart_cpuhistory{ 68 | 69 | 70 | } 71 | -------------------------------------------------------------------------------- /temp.php: -------------------------------------------------------------------------------- 1 | --------------------------------------------------------------------------------