9/09/2016

Python :: Python Check Speed Internet get Data for USE. PHP

Python :: Python Check Speed Internet get Data for USE.
1. Install follow This. http://porpramarn.blogspot.com/2016/09/python-python-check-speed-internet-on.html
2. Create File nettest.py
  1. nano nettest.py

3. Insert This Code.
  1. #!/usr/bin/python
  2. import os
  3. import sys
  4. import csv
  5. import datetime
  6. import time
  7. #import twitter
  8.  
  9. def test():
  10.  
  11.         #run speedtest-cli
  12.         print 'running test'
  13.         # /usr/bin/speedtest-cli
  14.         a = os.popen("python /usr/bin/speedtest-cli --simple").read()
  15.         # a = os.popen("python /home/pi/speedtest/speedtest-cli --simple").read()
  16.         print 'ran'
  17.         #split the 3 line result (ping,down,up)
  18.         lines = a.split('\n')
  19.         print a
  20.         ts = time.time()
  21.         date =datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
  22.         #if speedtest could not connect set the speeds to 0
  23.         if "Cannot" in a:
  24.                 p = 100
  25.                 d = 0
  26.                 u = 0
  27.         #extract the values for ping down and up values
  28.         else:
  29.                 p = lines[0][6:11]
  30.                 d = lines[1][10:14]
  31.                 u = lines[2][8:12]
  32.         print date,p, d, u
  33.         #save the data to file for local network plotting
  34.         # out_file = open('/var/www/assets/data.csv', 'a')
  35.         out_file = open('/home/pi/data.csv', 'a')
  36.  
  37.         writer = csv.writer(out_file)
  38.         writer.writerow((ts*1000,p,d,u))
  39.         out_file.close()
  40.         return
  41.  
  42. if __name__ == '__main__':
  43.         test()
  44.         print 'completed'
  45.  

4. Run python
  1. python nettest.py

Data
1454725814235.995,23.64,39.1,21.6

5. Use file data.csv Or Insert to database.

http://pastebin.com/WMEh802V

######################################################################
ติดตั้ง phpmyadmin เพิ่มต่อเพื่อดึงข้อมูลเข้า database เก็บไว้ใช้งานต่อไป
ติดตั้ง
  1. sudo apt-get install apache2
  1. sudo apt-get install mysql-server
  1. sudo apt-get install php5
  1. sudo apt-get install phpmyadmin


1. สร้าง ไฟล์ net-check.php ที่ var/www/
  1. nano /var/www/nano net-check.php

2. เพิ่มคนสำหรับ Check Speed Net ที่ Python และส่งเมล์เตือนเมื่อ Speed ได้ต่ำกว่ากำหนด Code
  1. <?php
  2. // Read Data From DHT22
  3. $strDateTime = date("Y-m-d H:i:s");
  4. //$strDhtData = exec("/usr/local/bin/Adafruit_DHT 22 23");
  5.  
  6. $command = escapeshellcmd('/home/pi/nettest.py');
  7. $output = shell_exec($command);
  8. //echo $output;
  9.  
  10. list($date, $time, $ping, $download, $upload) = explode(" ", $output);
  11.  
  12. echo $date.",".$time.",".$ping.",".$download.",".$upload;
  13.  
  14.         $fp = fopen('/var/www/data-net.txt', 'a+');
  15.         fwrite($fp, "Date = $, {$output}");
  16.         fclose($fp);
  17.  
  18.         if(floatval($download) < 50){
  19.                 $strToPost = "\nFrom Raspberry : Date = {$date} {$time} : Ping = {$ping} : Download = {$download} : Upload = {$upload} \n";
  20.                 //echo $strToPost;
  21. $ret = mail("komkid@gmail.com,suwit.jph@gmail.com,nontiwaza@gmail.com", "Internet Test Speed is low. Please Check Internet Connection.", $strToPost, "From: SCI Raspberry\n");
  22.                 //$ret = mail("suwit.jph@gmail.com","Internet Test Speed is low. Please Check Internet Connection.", $strToPost, "From: SCI Raspberry\n");
  23.                 if($ret)
  24.                         echo "OK {$output}";
  25.                 else
  26.                         echo "Failed!";
  27.         }
  28.  
  29. ?>
  30.  
  31.  


3. Chmod file pytho เพื่อให้ php สามารถ run ได้
  1. chmod +x nettest.py
ทดลอง Run ผ่าน Terminal แล้วดูผล
  1. /usr/bin/php /var/www/net-check.php

4. ตั้งเวลาให้ Run ทุก 1 ชั่วโมง โดยใช้คำสั่ง
  1. crontab -e

และเพิ่ม
  1. #Check Internet Every 1 Hours
  2. 0 * * * * /usr/bin/php /var/www/temp-alert.php

http://pastebin.com/WMEh802V

No comments:

Post a Comment