1. Install follow This. http://porpramarn.blogspot.com/2016/09/python-python-check-speed-internet-on.html
2. Create File nettest.py
- nano nettest.py
3. Insert This Code.
- #!/usr/bin/python
- import os
- import sys
- import csv
- import datetime
- import time
- #import twitter
- def test():
- #run speedtest-cli
- print 'running test'
- # /usr/bin/speedtest-cli
- a = os.popen("python /usr/bin/speedtest-cli --simple").read()
- # a = os.popen("python /home/pi/speedtest/speedtest-cli --simple").read()
- print 'ran'
- #split the 3 line result (ping,down,up)
- lines = a.split('\n')
- print a
- ts = time.time()
- date =datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
- #if speedtest could not connect set the speeds to 0
- if "Cannot" in a:
- p = 100
- d = 0
- u = 0
- #extract the values for ping down and up values
- else:
- p = lines[0][6:11]
- d = lines[1][10:14]
- u = lines[2][8:12]
- print date,p, d, u
- #save the data to file for local network plotting
- # out_file = open('/var/www/assets/data.csv', 'a')
- out_file = open('/home/pi/data.csv', 'a')
- writer = csv.writer(out_file)
- writer.writerow((ts*1000,p,d,u))
- out_file.close()
- return
- if __name__ == '__main__':
- test()
- print 'completed'
4. Run python
- 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 เก็บไว้ใช้งานต่อไป
ติดตั้ง
- sudo apt-get install apache2
- sudo apt-get install mysql-server
- sudo apt-get install php5
- sudo apt-get install phpmyadmin
1. สร้าง ไฟล์ net-check.php ที่ var/www/
- nano /var/www/nano net-check.php
2. เพิ่มคนสำหรับ Check Speed Net ที่ Python และส่งเมล์เตือนเมื่อ Speed ได้ต่ำกว่ากำหนด Code
- <?php
- // Read Data From DHT22
- $strDateTime = date("Y-m-d H:i:s");
- //$strDhtData = exec("/usr/local/bin/Adafruit_DHT 22 23");
- $command = escapeshellcmd('/home/pi/nettest.py');
- $output = shell_exec($command);
- //echo $output;
- list($date, $time, $ping, $download, $upload) = explode(" ", $output);
- echo $date.",".$time.",".$ping.",".$download.",".$upload;
- $fp = fopen('/var/www/data-net.txt', 'a+');
- fwrite($fp, "Date = $, {$output}");
- fclose($fp);
- if(floatval($download) < 50){
- $strToPost = "\nFrom Raspberry : Date = {$date} {$time} : Ping = {$ping} : Download = {$download} : Upload = {$upload} \n";
- //echo $strToPost;
- $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");
- //$ret = mail("suwit.jph@gmail.com","Internet Test Speed is low. Please Check Internet Connection.", $strToPost, "From: SCI Raspberry\n");
- if($ret)
- echo "OK {$output}";
- else
- echo "Failed!";
- }
- ?>
3. Chmod file pytho เพื่อให้ php สามารถ run ได้
- chmod +x nettest.py
- /usr/bin/php /var/www/net-check.php
4. ตั้งเวลาให้ Run ทุก 1 ชั่วโมง โดยใช้คำสั่ง
- crontab -e
และเพิ่ม
- #Check Internet Every 1 Hours
- 0 * * * * /usr/bin/php /var/www/temp-alert.php
http://pastebin.com/WMEh802V
No comments:
Post a Comment