-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathservices.py
More file actions
328 lines (296 loc) · 12.7 KB
/
services.py
File metadata and controls
328 lines (296 loc) · 12.7 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
"""Service layer for interacting with Icinga DB Web endpoints.
Author: Bernd Erk - Icinga GmbH
License: GPL-2.0-only
This module contains the IcingaDBService which encapsulates upstream HTTP calls
and exposes high-level methods consumed by the REST app and the MCP server.
All methods either return pass-through JSON data (lists) or ActionResult wrappers for actions.
"""
from __future__ import annotations
from typing import Any, cast
import structlog
from .config import Settings
from .filters import sanitize_query
from .http_client import IcingaWebClient
from .models import (
ActionResult,
Comment,
Downtime,
Event,
Host,
HostGroup,
Notification,
Service,
ServiceGroup,
)
log = structlog.get_logger(__name__)
class IcingaDBService:
def __init__(self, client: IcingaWebClient, settings: Settings):
self.c = client
self.s = settings
# Listings
async def list_hosts(
self,
*,
page: int | None = None,
limit: int | None = None,
extra: dict[str, Any] | None = None,
) -> list[Host]:
base = {"page": page, "limit": limit}
params = sanitize_query({**base, **(extra or {})})
data = await self.c.get_json(self.s.endpoint("hosts"), params=params)
# Accept passthrough shape but narrow type for the checker
return cast(list[Host], data)
async def list_services(
self,
*,
page: int | None = None,
limit: int | None = None,
extra: dict[str, Any] | None = None,
) -> list[Service]:
base = {"page": page, "limit": limit}
params = sanitize_query({**base, **(extra or {})})
data = await self.c.get_json(self.s.endpoint("services"), params=params)
return cast(list[Service], data)
async def list_downtimes(
self,
*,
page: int | None = None,
limit: int | None = None,
extra: dict[str, Any] | None = None,
) -> list[Downtime]:
base = {"page": page, "limit": limit}
params = sanitize_query({**base, **(extra or {})})
data = await self.c.get_json(self.s.endpoint("downtimes"), params=params)
return cast(list[Downtime], data)
async def list_notifications(
self,
*,
page: int | None = None,
limit: int | None = None,
extra: dict[str, Any] | None = None,
) -> list[Notification]:
base = {"page": page, "limit": limit}
params = sanitize_query({**base, **(extra or {})})
data = await self.c.get_json(self.s.endpoint("notifications"), params=params)
return cast(list[Notification], data)
async def list_comments(
self,
*,
page: int | None = None,
limit: int | None = None,
extra: dict[str, Any] | None = None,
) -> list[Comment]:
base = {"page": page, "limit": limit}
params = sanitize_query({**base, **(extra or {})})
data = await self.c.get_json(self.s.endpoint("comments"), params=params)
return cast(list[Comment], data)
async def list_hostgroups(
self,
*,
page: int | None = None,
limit: int | None = None,
extra: dict[str, Any] | None = None,
) -> list[HostGroup]:
base = {"page": page, "limit": limit}
extra = extra or {}
use_detail = "name" in extra
params = sanitize_query({**base, **extra})
endpoint_key = "hostgroup" if use_detail else "hostgroups"
data = await self.c.get_json(self.s.endpoint(endpoint_key), params=params)
return cast(list[HostGroup], data)
async def list_servicegroups(
self,
*,
page: int | None = None,
limit: int | None = None,
extra: dict[str, Any] | None = None,
) -> list[ServiceGroup]:
base = {"page": page, "limit": limit}
extra = extra or {}
use_detail = "name" in extra
params = sanitize_query({**base, **extra})
endpoint_key = "servicegroup" if use_detail else "servicegroups"
data = await self.c.get_json(self.s.endpoint(endpoint_key), params=params)
return cast(list[ServiceGroup], data)
async def list_host_history(
self,
*,
page: int | None = None,
limit: int | None = None,
extra: dict[str, Any] | None = None,
) -> list[Event]:
base = {"page": page, "limit": limit}
params = sanitize_query({**base, **(extra or {})})
data = await self.c.get_json(self.s.endpoint("host_history"), params=params)
return cast(list[Event], data)
async def list_service_history(
self,
*,
page: int | None = None,
limit: int | None = None,
extra: dict[str, Any] | None = None,
) -> list[Event]:
base = {"page": page, "limit": limit}
params = sanitize_query({**base, **(extra or {})})
data = await self.c.get_json(self.s.endpoint("service_history"), params=params)
return cast(list[Event], data)
async def add_host_comment(self, *, name: str, comment: str, expire: str = "n") -> ActionResult:
params = {"name": name}
form_fields = {"comment": comment, "expire": expire}
resp = await self.c.request(
"POST", self.s.endpoint("host_add_comment"), params=params, form_fields=form_fields
)
resp.raise_for_status()
detail = resp.json() if resp.content else {}
return ActionResult(ok=True, upstream_status=resp.status_code, detail=detail)
async def add_service_comment(
self, *, name: str, host_name: str, comment: str, expire: str = "n"
) -> ActionResult:
params = {"name": name, "host.name": host_name}
form_fields = {"comment": comment, "expire": expire}
resp = await self.c.request(
"POST", self.s.endpoint("service_add_comment"), params=params, form_fields=form_fields
)
resp.raise_for_status()
detail = resp.json() if resp.content else {}
return ActionResult(ok=True, upstream_status=resp.status_code, detail=detail)
async def acknowledge_host(self, *, name: str, comment: str) -> ActionResult:
params = {"name": name}
form_fields = {
"comment": comment,
"persistent": "n",
"notify": "y",
"sticky": "n",
"expire": "n",
}
resp = await self.c.request(
"POST", self.s.endpoint("host_acknowledge"), params=params, form_fields=form_fields
)
resp.raise_for_status()
detail = resp.json() if resp.content else {}
return ActionResult(ok=True, upstream_status=resp.status_code, detail=detail)
async def acknowledge_service(self, *, name: str, host_name: str, comment: str) -> ActionResult:
params = {"name": name, "host.name": host_name}
form_fields = {
"comment": comment,
"persistent": "n",
"notify": "y",
"sticky": "n",
"expire": "n",
}
resp = await self.c.request(
"POST", self.s.endpoint("service_acknowledge"), params=params, form_fields=form_fields
)
resp.raise_for_status()
detail = resp.json() if resp.content else {}
return ActionResult(ok=True, upstream_status=resp.status_code, detail=detail)
async def remove_acknowledgement_host(self, *, name: str) -> ActionResult:
params = {"name": name}
resp = await self.c.request(
"POST", self.s.endpoint("host_remove_acknowledgement"), params=params
)
resp.raise_for_status()
detail = resp.json() if resp.content else {}
return ActionResult(ok=True, upstream_status=resp.status_code, detail=detail)
async def remove_acknowledgement_service(self, *, name: str, host_name: str) -> ActionResult:
params = {"name": name, "host.name": host_name}
resp = await self.c.request(
"POST", self.s.endpoint("service_remove_acknowledgement"), params=params
)
resp.raise_for_status()
detail = resp.json() if resp.content else {}
return ActionResult(ok=True, upstream_status=resp.status_code, detail=detail)
async def remove_comment(self, payload: dict[str, Any]) -> ActionResult:
# Prefer the 'name' field from the comments list; accept legacy keys too
comment_name = (
payload.get("name") or payload.get("comment_name") or payload.get("comment.name")
)
if not comment_name:
host = payload.get("host") or payload.get("host.name")
service = payload.get("service") or payload.get("service.name")
comment_id = payload.get("id") or payload.get("comment_id") or payload.get("commentId")
if not host or not comment_id:
raise ValueError(
"remove_comment requires 'name' (from list) or 'comment.name'; alternatively provide 'host' and 'id' (optional 'service')."
)
comment_name = f"{host}!{service}!{comment_id}" if service else f"{host}!{comment_id}"
params = sanitize_query({"comment.name": comment_name})
resp = await self.c.request("POST", self.s.endpoint("comments_delete"), params=params)
resp.raise_for_status()
detail = resp.json() if resp.content else {}
return ActionResult(ok=True, upstream_status=resp.status_code, detail=detail)
async def remove_downtime(self, payload: dict[str, Any]) -> ActionResult:
downtime_name = (
payload.get("name") or payload.get("downtime_name") or payload.get("downtime.name")
)
if not downtime_name:
host = payload.get("host") or payload.get("host.name")
service = payload.get("service") or payload.get("service.name")
downtime_id = (
payload.get("id") or payload.get("downtime_id") or payload.get("downtimeId")
)
if not host or not downtime_id:
raise ValueError(
"remove_downtime requires 'name' (from list) or 'downtime.name'; alternatively provide 'host' and 'id' (optional 'service')."
)
downtime_name = (
f"{host}!{service}!{downtime_id}" if service else f"{host}!{downtime_id}"
)
params = sanitize_query({"downtime.name": downtime_name})
resp = await self.c.request("POST", self.s.endpoint("downtimes_delete"), params=params)
resp.raise_for_status()
detail = resp.json() if resp.content else {}
return ActionResult(ok=True, upstream_status=resp.status_code, detail=detail)
async def check_now_host(self, *, name: str) -> ActionResult:
params = {"name": name}
resp = await self.c.request("POST", self.s.endpoint("host_check_now"), params=params)
resp.raise_for_status()
detail = resp.json() if resp.content else {}
return ActionResult(ok=True, upstream_status=resp.status_code, detail=detail)
async def check_now_service(self, *, name: str, host_name: str) -> ActionResult:
params = {"name": name, "host.name": host_name}
resp = await self.c.request("POST", self.s.endpoint("service_check_now"), params=params)
resp.raise_for_status()
detail = resp.json() if resp.content else {}
return ActionResult(ok=True, upstream_status=resp.status_code, detail=detail)
async def schedule_downtime_host(
self, *, name: str, comment: str, start: str, end: str
) -> ActionResult:
params = {"name": name}
form_fields = {
"comment": comment,
"start": start,
"end": end,
"flexible": "n",
"all_services": "n",
"child_options": "0",
}
resp = await self.c.request(
"POST",
self.s.endpoint("host_schedule_downtime"),
params=params,
form_fields=form_fields,
)
resp.raise_for_status()
detail = resp.json() if resp.content else {}
return ActionResult(ok=True, upstream_status=resp.status_code, detail=detail)
async def schedule_downtime_service(
self, *, name: str, host_name: str, comment: str, start: str, end: str
) -> ActionResult:
params = {"name": name, "host.name": host_name}
form_fields = {
"comment": comment,
"start": start,
"end": end,
"flexible": "n",
"child_options": "0",
}
resp = await self.c.request(
"POST",
self.s.endpoint("service_schedule_downtime"),
params=params,
form_fields=form_fields,
)
resp.raise_for_status()
detail = resp.json() if resp.content else {}
return ActionResult(ok=True, upstream_status=resp.status_code, detail=detail)