Add lua serverd
authorAndrew Andrianov <andrew@ncrmnt.org>
Fri, 13 Feb 2015 19:22:18 +0000 (22:22 +0300)
committerAndrew Andrianov <andrew@ncrmnt.org>
Fri, 13 Feb 2015 19:22:18 +0000 (22:22 +0300)
Signed-off-by: Andrew Andrianov <andrew@ncrmnt.org>
extra/serverd.lua [new file with mode: 0644]

diff --git a/extra/serverd.lua b/extra/serverd.lua
new file mode 100644 (file)
index 0000000..5d61408
--- /dev/null
@@ -0,0 +1,88 @@
+#!/usr/bin/lua
+
+
+--TODO: host.conf
+hosts = { 
+   {"192.168.0.1", "windblade"},
+   {"192.168.0.20", "iceblade"},
+}
+sleep_interval=1
+
+--dofile("/etc/serverd.conf")
+
+function gpio(host, n,o)
+   local str="cp2103gpio --serial="..host.." --gpio="..n.." --out="..o
+   os.execute(str)
+   --print(str)
+end
+
+function green(host, state)
+   if (state) then
+      gpio(host, 0, 0);
+   else
+      gpio(host, 0, 1);
+   end
+end
+
+function ylw(host, state)
+   if (state) then
+      gpio(host, 1, 0);
+   else
+      gpio(host, 1, 1);
+   end
+end
+
+function red(host, state)
+   if (state) then
+      gpio(host, 2, 0);
+   else
+      gpio(host, 2, 1);
+   end
+end
+
+function psu(host, state)
+   if (state) then
+      gpio(host, 2, 1);
+   else
+      gpio(host, 2, 0);
+   end
+end
+
+
+function is_alive(host, name)
+   ylw(name, true)
+   result = os.execute("ping -w 1 -c 1 "..host.." > /dev/null") 
+   ylw(name, false)
+   if (0==result) then 
+      return true;
+   else
+      return false;   
+   end
+
+end
+
+function sleep(n)
+   ret = os.execute("sleep "..n);
+   if (ret > 0) then
+      error("Interrupted!");
+   end
+end
+
+
+function check_host_status(host, name)
+   if (is_alive(host, name)) then
+      green(name, true)
+      red(name, false)
+   else
+      green(name, false)
+      red(name, true)
+   end
+end
+
+while true do 
+   for i,j in pairs(hosts) do
+      check_host_status(j[1],j[2])
+      sleep(sleep_interval)
+   end
+
+end
\ No newline at end of file