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