-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetc_hosts_to_yaml.py
38 lines (32 loc) · 1.01 KB
/
etc_hosts_to_yaml.py
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
import yaml
import argparse
if __name__ == "__main__":
# parse args
parser = argparse.ArgumentParser("sitemon options")
parser.add_argument("hostsPath", help="source hosts file")
parser.add_argument("destPath", help="dest yaml file")
args = parser.parse_args()
hosts = []
with open(args.hostsPath, "r") as f:
for line in f.readlines():
# コメントがあったらそれ以降を切る
if "#" in line:
commentOutIndex = line.index("#")
line = line[:commentOutIndex]
words = line.split()
if len(words) < 2:
continue
host = {
"name": words[1].strip(),
"value": words[0].strip(),
"type": "icmp"
}
hosts.append(host)
resultDict = {
"slack": {
"url": "CHANGEME"
},
"targets": hosts
}
with open(args.destPath, "w") as f:
yaml.dump(resultDict, f)