This repository has been archived by the owner on Feb 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
centreon-plugin
executable file
·193 lines (132 loc) · 5.89 KB
/
centreon-plugin
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2014-2017 Normation SAS
#
# This file is a module of Rudder.
#
# Rudder is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# You should have received a copy of the GNU General Public License
# along with Rudder. If not, see <http://www.gnu.org/licenses/>.
# Name: centreon-plugin
# Synopsis: Export Nodes which have a configured centreon or snmp agent, and add them in Centreon using CLAPI
# Usage: Run centreon-plugin regularly
# You need to set:
# api_token variable to the Rudder token API
# snmp_groupid variable to the id of the group having snmp node configured, and they'll have the OS-Linux-SNMP;Central template applied in Centreon
# centron_groupid variable to the id of the group having centreon agent configured, and they'll have the OS-Linux-NRPE;Central template applied in Centreon
# Imports - builtins
import os.path
import json
import subprocess
import csv
# Imports - external
import requests
# Configuration - BEGIN
api_url = "https://localhost/rudder/api"
api_token = "XXXXXXXXXXXXXXXXXXXXXXXXx"
validate_https = False
snmp_groupid = "e73f91de-2182-437c-8560-adf5ec517fb1" # group having snmp nodes configured
centreon_groupid = "9bfc73a7-ef57-4205-a958-5da7b0179d27" # group having centreon agent configured
# Configuration - END
# Global variables
api_version = 4
snmp_url = "%s/%s/groups/%s" % (api_url, api_version, snmp_groupid)
centreon_url = "%s/%s/groups/%s" % (api_url, api_version, centreon_groupid)
node_url = "%s/%s/nodes" % (api_url, api_version)
headers = {"X-API-Token": api_token}
# SNMP NODES
# Call API
snmp_r = requests.get(snmp_url, headers=headers, verify=validate_https)
# Parse JSON
snmp_j = json.loads(snmp_r.content)
# Define nodes
snmp_nodesid = snmp_j["data"]["groups"][0]["nodeIds"]
snmp_nodes = {}
for nodeid in snmp_nodesid:
snmp_node_r = requests.get(node_url + "/" + nodeid , headers=headers, verify=validate_https)
snmp_node_j = json.loads(snmp_node_r.content)
snmp_nodes[snmp_node_j["data"]["nodes"][0]["ipAddresses"][2]] = snmp_node_j["data"]["nodes"][0]["hostname"]
# CENTREON NODES
# Call API
centreon_r = requests.get(centreon_url, headers=headers, verify=validate_https)
# Parse JSON
centreon_j = json.loads(centreon_r.content)
# Define nodes
centreon_nodesid = centreon_j["data"]["groups"][0]["nodeIds"]
centreon_nodes = {}
for nodeid in centreon_nodesid:
centreon_node_r = requests.get(node_url + "/" + nodeid , headers=headers, verify=validate_https)
centreon_node_j = json.loads(centreon_node_r.content)
centreon_nodes[centreon_node_j["data"]["nodes"][0]["ipAddresses"][2]] = centreon_node_j["data"]["nodes"][0]["hostname"]
all_nodes = dict(centreon_nodes.items() + snmp_nodes.items())
#print("********** SNMP")
#print(snmp_nodes)
#print("********** CENTREON")
#print(centreon_nodes)
#print("********** ALL")
#print(all_nodes)
# List all nodes on Centreon
centreon_node_csv = os.popen("centreon -u admin -p admin -o HOST -a show")
reader = csv.reader(centreon_node_csv, delimiter=';')
registered_nodes = {}
for entry in reader:
registered_nodes[entry[3]] = entry[2]
# Missing snmp nodes
missing_snmp_nodes = set(snmp_nodes.keys()) - set(registered_nodes.keys())
missing_centreon_nodes = set(centreon_nodes.keys()) - set(registered_nodes.keys())
print "missing snmp nodes"
print missing_snmp_nodes
update_conf = 0
for add_snmp in missing_snmp_nodes:
cmd = "centreon -u admin -p admin -o HOST -a ADD -v \"" + snmp_nodes[add_snmp] + ";" + snmp_nodes[add_snmp] + ";" + add_snmp + ";OS-Linux-SNMP;Central;\""
result_snmp = os.popen(cmd)
update_tpl = "centreon -u admin -p admin -o HOST -a applytpl -v \""+ snmp_nodes[add_snmp] +"\""
result_tpl = os.popen(update_tpl)
update_conf = 1
#print result_snmp
for add_centreon in missing_centreon_nodes:
cmd = "centreon -u admin -p admin -o HOST -a ADD -v \"" + centreon_nodes[add_centreon] + ";" + centreon_nodes[add_centreon] + ";" + add_centreon + ";OS-Linux-NRPE;Central;\""
result_centreon = os.popen(cmd)
update_tpl = "centreon -u admin -p admin -o HOST -a applytpl -v \""+ centreon_nodes[add_centreon] +"\""
result_tpl = os.popen(update_tpl)
#print update_tpl
#print result_tpl
update_conf = 1
#print result_centreon
generate = "centreon -u admin -p admin -a APPLYCFG -v 1"
reload = "centreon -u admin -p admin -a POLLERRESTART -v 1"
if update_conf == 1:
print "Regererating conf"
apply = os.popen(generate)
for add_centreon in missing_centreon_nodes:
update_tpl = "centreon -u admin -p admin -o HOST -a applytpl -v \""+ centreon_nodes[add_centreon] +"\""
result_tpl = os.popen(update_tpl)
for add_snmp in missing_snmp_nodes:
update_tpl = "centreon -u admin -p admin -o HOST -a applytpl -v \""+ snmp_nodes[add_snmp] +"\""
result_tpl = os.popen(update_tpl)
apply = os.popen(generate)
reload = os.popen(reload)
for add_snmp in missing_snmp_nodes:
update_tpl = "centreon -u admin -p admin -o HOST -a applytpl -v \""+ snmp_nodes[add_snmp] +"\""
result_tpl = os.popen(update_tpl)
update_conf = 1
for add_centreon in missing_centreon_nodes:
update_tpl = "centreon -u admin -p admin -o HOST -a applytpl -v \""+ centreon_nodes[add_centreon] +"\""
result_tpl = os.popen(update_tpl)
update_conf = 1
if update_conf == 1:
print "Regererating conf"
apply = os.popen(generate)
for add_centreon in missing_centreon_nodes:
update_tpl = "centreon -u admin -p admin -o HOST -a applytpl -v \""+ centreon_nodes[add_centreon] +"\""
result_tpl = os.popen(update_tpl)
for add_snmp in missing_snmp_nodes:
update_tpl = "centreon -u admin -p admin -o HOST -a applytpl -v \""+ snmp_nodes[add_snmp] +"\""
result_tpl = os.popen(update_tpl)
apply = os.popen(generate)
reload = os.popen(reload)