extra: add serverctl script
[pub/pl2303-ft232-gpio.git] / extra / serverd.lua
1 #!/usr/bin/lua
2
3
4 --TODO: host.conf
5 hosts = {
6 {"192.168.0.1", "windblade"},
7 {"192.168.0.20", "iceblade"},
8 }
9 sleep_interval=1
10
11 --dofile("/etc/serverd.conf")
12
13 function gpio(host, n,o)
14 local str="cp2103gpio --serial="..host.." --gpio="..n.." --out="..o
15 os.execute(str)
16 --print(str)
17 end
18
19 function green(host, state)
20 if (state) then
21 gpio(host, 0, 0);
22 else
23 gpio(host, 0, 1);
24 end
25 end
26
27 function ylw(host, state)
28 if (state) then
29 gpio(host, 1, 0);
30 else
31 gpio(host, 1, 1);
32 end
33 end
34
35 function red(host, state)
36 if (state) then
37 gpio(host, 2, 0);
38 else
39 gpio(host, 2, 1);
40 end
41 end
42
43 function psu(host, state)
44 if (state) then
45 gpio(host, 2, 1);
46 else
47 gpio(host, 2, 0);
48 end
49 end
50
51
52 function is_alive(host, name)
53 ylw(name, true)
54 result = os.execute("ping -w 1 -c 1 "..host.." > /dev/null")
55 ylw(name, false)
56 if (0==result) then
57 return true;
58 else
59 return false;
60 end
61
62 end
63
64 function sleep(n)
65 ret = os.execute("sleep "..n);
66 if (ret > 0) then
67 error("Interrupted!");
68 end
69 end
70
71
72 function check_host_status(host, name)
73 if (is_alive(host, name)) then
74 green(name, true)
75 red(name, false)
76 else
77 green(name, false)
78 red(name, true)
79 end
80 end
81
82 while true do
83 for i,j in pairs(hosts) do
84 check_host_status(j[1],j[2])
85 sleep(sleep_interval)
86 end
87
88 end