-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor-manage
executable file
·116 lines (89 loc) · 2.87 KB
/
monitor-manage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#! /usr/bin/python3
"""
Manage monitored hosts by creating/deleting symbolic links from
/etc/naemon/hosts.enabled to /etc/naemon/hosts.available and
will restart naemon.
This version of the program does not check naemon to insure
it has started without error.
The program is invoked by using symbolic links. The links are named:
enhost -- enable a host
dishost -- disable a host
listhosts -- list all hosts
"""
NAEMON_ETC = '/etc/naemon'
NAEMON_CFG = NAEMON_ETC + '/naemon.cfg'
NAEMON_AVAILABLE = NAEMON_ETC + '/hosts.available'
NAEMON_ENABLED = NAEMON_ETC + '/hosts.enabled'
NAEMON_CFG_SUFFIX = ".cfg"
NAEMON_INCLUDE = "include="
import argparse
import sys
import os
from os.path import isfile,join
import subprocess
def find(name,path):
for root,dirs,files in os.walk(path):
if name + NAEMON_CFG_SUFFIX in files:
return os.path.join(root,name + NAEMON_CFG_SUFFIX)
else:
return None
def restart():
"""
restart naemon
"""
subprocess.run(['/bin/systemctl','restart', 'naemon'])
def enhost(host):
"""
First, search the available hosts directory for the
desired host. If it doesn't exist, exit...
Next, search the enabled hosts directory for the
desired host. If it exists, exit...
Create a symbolic link in the enabled directory.
(optional) restart naemon
"""
available = find(host,NAEMON_AVAILABLE)
if not available:
print("%s not found in available directory -- " % host + NAEMON_AVAILABLE)
return
enabled = find(host,NAEMON_ENABLED)
if enabled:
print("%s is already enabled" % host)
return
src = available
dest = NAEMON_ENABLED + '/' + host + NAEMON_CFG_SUFFIX
print("Linking %s to %s" % (src,dest))
os.symlink(src,dest)
restart()
listhosts()
def dishost(host):
available = find(host,NAEMON_AVAILABLE)
if not available:
print("%s not found in available directory -- " % host + NAEMON_AVAILABLE)
enabled = find(host,NAEMON_ENABLED)
if not enabled:
print("%s is not enabled" % host)
return
print("Removing %s" % enabled)
os.remove(enabled)
restart()
listhosts()
def listhosts():
print("Available:")
files = [f for f in os.listdir(NAEMON_AVAILABLE) if isfile(join(NAEMON_AVAILABLE,f))]
for file in files:
print("\t" + file)
print("Enabled:")
files = [f for f in os.listdir(NAEMON_ENABLED) if isfile(join(NAEMON_ENABLED,f))]
for file in files:
print("\t" + file)
if __name__ == "__main__":
command = os.path.basename(sys.argv[0])
if command == 'monitor-enable':
enhost(sys.argv[1])
elif command == 'monitor-disable':
dishost(sys.argv[1])
elif command == 'monitor-list':
listhosts()
else:
print("Use monitor-enable, monitor-disable, monitor-list instead")
sys.exit()