daily pastebin goal
25%
SHARE
TWEET

Untitled

a guest Oct 19th, 2018 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env lua
  2.  
  3. local socket = require("socket")
  4. local use_ssl, ssl = pcall(require, "ssl")
  5.  
  6. local Blynk = require("blynk.socket")
  7. local Timer = require("timer")
  8.  
  9. assert(#arg >= 1, "Please specify Auth Token")
  10. local auth = arg[1]
  11.  
  12. local blynk = Blynk.new(auth, {
  13.   heartbeat = 30, -- default h-beat is 30
  14.   log = print,
  15. })
  16.  
  17. function exec_out(cmd)
  18.   local file = io.popen(cmd)
  19.   if not file then return nil end
  20.   local output = file:read('*all')
  21.   file:close()
  22.   -- print("Run: "..cmd.." -> "..output)
  23.   return output
  24. end
  25.  
  26. function read_file(path)
  27.   local file = io.open(path, "rb")
  28.   if not file then return nil end
  29.   local content = file:read "*a"
  30.   file:close()
  31.   -- print("Read: "..path.." -> "..content)
  32.   return content
  33. end
  34.  
  35. function getArpClients()
  36.   return tonumber(exec_out("cat /proc/net/arp | grep br-lan | grep 0x2 | wc -l"))
  37. end
  38.  
  39. function getUptime()
  40.   return tonumber(exec_out("cat /proc/uptime | awk '{print $1}'"))
  41. end
  42.  
  43. function getWanIP()
  44.   return exec_out("ifconfig eth0.2 | grep 'inet addr:' | cut -d: -f2 | awk '{print $1}'")
  45. end
  46.  
  47. function getHddSpinning()
  48.   local code = os.execute("smartctl --nocheck=standby --info /dev/sda > /dev/null")
  49.   return (code == 0) and 1 or 0
  50. end
  51.  
  52. function getHddStats()
  53.   local out = exec_out("cat /proc/diskstats | grep 'sda ' | awk '{print $6, $10}'")
  54.   local reads, writes = out:match("([^ ]+) +([^ ]+)")
  55.   return reads, writes
  56. end
  57.  
  58. function getCpuLoad()
  59.   return tonumber(exec_out("top -bn1 | grep 'CPU:' | head -n1 | awk '{print $2+$4}'"))
  60. end
  61.  
  62. function getRamUsage()
  63.   return tonumber(exec_out("free | grep Mem | awk '{print ($3-$7)/$2 * 100.0}'"))
  64. end
  65.  
  66. function getWanRxBytes()
  67.   return tonumber(read_file("/sys/class/net/eth0.2/statistics/rx_bytes"))
  68. end
  69.  
  70. function getWanTxBytes()
  71.   return tonumber(read_file("/sys/class/net/eth0.2/statistics/tx_bytes"))
  72. end
  73.  
  74. local function connectBlynk()
  75.   local host = "blynk-cloud.com"
  76.  
  77.   local sock = assert(socket.tcp())
  78.   sock:setoption("tcp-nodelay", true)
  79.  
  80.   if use_ssl then
  81.     print("Connecting Blynk (secure)...")
  82.     sock:connect(host, 8441)
  83.     local opts = {
  84.       mode = "client",
  85.       protocol = "tlsv1"
  86.     }
  87.     sock = assert(ssl.wrap(sock, opts))
  88.     sock:dohandshake()
  89.   else
  90.     print("Connecting Blynk...")
  91.     sock:connect(host, 80)
  92.   end
  93.  
  94.   -- tell Blynk to use this socket
  95.   blynk:connect(sock)
  96. end
  97.  
  98. --[[ WiFi control ]]
  99.  
  100. blynk:on("V20", function(param)
  101.   if param[1] == "1" then
  102.     os.execute("wifi up")
  103.   else
  104.     os.execute("wifi down")
  105.   end
  106. end)
  107.  
  108. --[[ Locking ]]
  109.  
  110. local isLocked = true
  111.  
  112. local function advLock()
  113.   blynk:setProperty(30,"color","#D3435C") -- red
  114.   blynk:setProperty(30,"label","locked")
  115.   isLocked = true
  116. end
  117.  
  118. local function advUnlock()
  119.   blynk:setProperty(30,"color","#23C48E") -- green
  120.   blynk:setProperty(30,"label","unlocked")
  121.   isLocked = false
  122. end
  123.  
  124. blynk:on("V30", function(param)
  125.   if isLocked and param[1] == "10" then
  126.     advUnlock()
  127.   elseif not isLocked then
  128.     advLock()
  129.   end
  130. end)
  131.  
  132. --[[ Reboot ]]
  133.  
  134. blynk:on("V31", function(param)
  135.   if param[1] == "1" then
  136.     if isLocked then
  137.       blynk:virtualWrite(35, "Reboot is LOCKED\n")
  138.       return
  139.     end
  140.     os.execute("reboot")
  141.   end
  142. end)
  143.  
  144. --[[ Shell ]]
  145.  
  146. blynk:on("V35", function(param)
  147.   if isLocked then
  148.     blynk:virtualWrite(35, "Shell is LOCKED\n")
  149.     return
  150.   end
  151.   local out = exec_out(param[1])
  152.   blynk:virtualWrite(35, out)
  153. end)
  154.  
  155.  
  156.  
  157. blynk:on("connected", function(ping)
  158.   print("Ready. Ping: "..math.floor(ping*1000).."ms")
  159.  
  160.   blynk:virtualWrite(12, getWanIP())
  161. end)
  162.  
  163. blynk:on("disconnected", function()
  164.   print("Disconnected.")
  165.   -- auto-reconnect after 5 seconds
  166.   socket.sleep(5)
  167.   connectBlynk()
  168. end)
  169.  
  170. --[[ Timers ]]
  171.  
  172. local prev = { spin = -1 }
  173.  
  174. local tmr1 = Timer:new{interval = 5000, func = function()
  175.   local tx = getWanTxBytes()
  176.   local rx = getWanRxBytes()
  177.  
  178.   if prev.tx then
  179.     local dtx = tx - prev.tx
  180.     if prev.dtx ~= dtx then
  181.       blynk:virtualWrite(1, dtx)
  182.       prev.dtx = dtx
  183.     end
  184.   end
  185.   prev.tx = tx
  186.  
  187.   if prevRx and prevRx ~= rx then
  188.     local drx = rx - prev.rx
  189.     if prev.drx ~= drx then
  190.       blynk:virtualWrite(2, drx)
  191.       prev.drx = drx
  192.     end
  193.   end
  194.   prev.rx = rx
  195.  
  196.   local spin = getHddSpinning()
  197.   if spin ~= prev.spin then
  198.     blynk:virtualWrite(7, spin)
  199.   end
  200.   prev.spin = spin
  201.  
  202.   if spin then
  203.     local rd, wt = getHddStats()
  204.  
  205.     if prev.rd then
  206.       local drd = rd - prev.rd
  207.       if prev.drd ~= drd then
  208.         blynk:virtualWrite(8, drd)
  209.         prev.drd = drd
  210.       end
  211.     end
  212.     prev.rd = rd
  213.    
  214.     if prev.wt then
  215.       local dwt = wt - prev.wt
  216.       if prev.dwt ~= dwt then
  217.         blynk:virtualWrite(9, dwt)
  218.         prev.dwt = dwt
  219.       end
  220.     end
  221.     prev.wt = wt
  222.   end
  223.  
  224.   blynk:virtualWrite(5, getCpuLoad())
  225.   blynk:virtualWrite(6, getRamUsage())
  226. end}
  227.  
  228. local tmr2 = Timer:new{interval = 5*60*1000, func = function()
  229.   blynk:virtualWrite(10, getArpClients())
  230.   blynk:virtualWrite(11, string.format("%.1f h", getUptime()/60/60))
  231. end}
  232.  
  233. connectBlynk()
  234.  
  235. while true do
  236.   blynk:run()
  237.   tmr1:run()
  238.   tmr2:run()
  239. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top