command_watcher 0.1.0__tar.gz → 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: command-watcher
3
- Version: 0.1.0
3
+ Version: 0.2.0
4
4
  Summary: Module to watch the execution of shell scripts. Both streams (`stdout` and `stderr`) are captured.
5
5
  Home-page: https://github.com/Josef-Friedrich/command-watcher
6
6
  License: MIT
@@ -14,8 +14,7 @@ Classifier: Programming Language :: Python :: 3.8
14
14
  Classifier: Programming Language :: Python :: 3.9
15
15
  Classifier: Programming Language :: Python :: 3.10
16
16
  Requires-Dist: conf2levels (>=0.4.0,<0.5.0)
17
- Requires-Dist: jflib (>=1.0.0,<2.0.0)
18
- Requires-Dist: requests (>=2.28.1,<3.0.0)
17
+ Requires-Dist: icinga2apic (>=0.7.5,<0.8.0)
19
18
  Requires-Dist: termcolor (>=1.1.0,<2.0.0)
20
19
  Requires-Dist: types-requests (>=2.28.1,<3.0.0)
21
20
  Requires-Dist: types-termcolor (>=1.1.5,<2.0.0)
@@ -11,11 +11,11 @@ import shutil
11
11
  import subprocess
12
12
  import threading
13
13
  import time
14
- from typing import (IO, Any, Dict, List, Optional, Sequence, Tuple, TypedDict,
15
- Union)
14
+ from importlib import metadata
15
+ from typing import (IO, Any, Dict, List, Literal, Optional, Sequence, Tuple,
16
+ TypedDict, Union)
16
17
 
17
18
  from conf2levels import ClassInterface, ConfigReader, Spec
18
- from jflib import capturing
19
19
  from typing_extensions import Unpack
20
20
 
21
21
  from .log import ExtendedLogger, LoggingHandler, setup_logging
@@ -23,6 +23,10 @@ from .report import (HOSTNAME, BaseChannel, BeepChannel, EmailChannel,
23
23
  IcingaChannel, Message, MessageParams,
24
24
  MinimalMessageParams, Status, reporter)
25
25
 
26
+ __version__: str = metadata.version('command_watcher')
27
+
28
+ Stream = Literal['stdout', 'stderr']
29
+
26
30
 
27
31
  class CommandWatcherError(Exception):
28
32
  """Exception raised by this module."""
@@ -173,7 +177,7 @@ class Process:
173
177
  args: Args
174
178
  """Process arguments in various types."""
175
179
 
176
- _queue: 'queue.Queue[Optional[Tuple[bytes, capturing.Stream]]]'
180
+ _queue: 'queue.Queue[Optional[Tuple[bytes, Stream]]]'
177
181
 
178
182
  log: ExtendedLogger
179
183
  """A ready to go and configured logger."""
@@ -251,7 +255,7 @@ class Process:
251
255
  return len(self.stderr.splitlines())
252
256
 
253
257
  def _stdout_stderr_reader(self, pipe: IO[bytes],
254
- stream: capturing.Stream) -> None:
258
+ stream: Stream) -> None:
255
259
  """
256
260
  :param object pipe: `process.stdout` or `process.stdout`
257
261
  """
@@ -263,7 +267,7 @@ class Process:
263
267
  self._queue.put(None)
264
268
 
265
269
  def _start_thread(self, pipe: Optional[IO[str]],
266
- stream: capturing.Stream) -> None:
270
+ stream: Stream) -> None:
267
271
  """
268
272
  :param object pipe: `process.stdout` or `process.stdout`
269
273
  """
@@ -0,0 +1,37 @@
1
+ from typing import Literal, Optional
2
+
3
+ import urllib3
4
+ from icinga2apic.client import Client
5
+
6
+ urllib3.disable_warnings() # type: ignore
7
+
8
+
9
+ STATE_OK = 0
10
+ STATE_WARNING = 1
11
+ STATE_CRITICAL = 2
12
+ STATE_UNKNOWN = 3
13
+
14
+ States = {
15
+ STATE_OK: 'OK',
16
+ STATE_WARNING: 'WARNING',
17
+ STATE_CRITICAL: 'CRITICAL',
18
+ STATE_UNKNOWN: 'UNKNOWN',
19
+ }
20
+
21
+
22
+ def send_passive_check(url: str, user: str, password: str,
23
+ status: Literal[0, 1, 2, 3],
24
+ host_name: str, service_name: str,
25
+ text_output: str,
26
+ performance_data: Optional[str] = None
27
+ ):
28
+ """
29
+ https://icinga.com/docs/icinga-2/latest/doc/12-icinga2-api/#process-check-result
30
+ """
31
+ client = Client(url=url,
32
+ username=user, password=password)
33
+ return client.actions.process_check_result(
34
+ object_type='Service',
35
+ name='{}!{}'.format(host_name, service_name),
36
+ exit_status=status,
37
+ plugin_output=text_output, performance_data=performance_data)
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "command_watcher"
3
- version = "0.1.0"
3
+ version = "0.2.0"
4
4
  description = "Module to watch the execution of shell scripts. Both streams (`stdout` and `stderr`) are captured."
5
5
  authors = ["Josef Friedrich <josef@friedrich.rocks>"]
6
6
  license = "MIT"
@@ -13,12 +13,11 @@ classifiers = [
13
13
 
14
14
  [tool.poetry.dependencies]
15
15
  python = "^3.8"
16
- termcolor = "^1.1.0"
17
- types-termcolor = "^1.1.5"
18
16
  conf2levels = "^0.4.0"
19
- requests = "^2.28.1"
20
- jflib = "^1.0.0"
17
+ icinga2apic = "^0.7.5"
18
+ termcolor = "^1.1.0"
21
19
  types-requests = "^2.28.1"
20
+ types-termcolor = "^1.1.5"
22
21
 
23
22
  [tool.poetry.group.dev.dependencies]
24
23
  tox = "^3.25.1"
@@ -9,15 +9,14 @@ package_data = \
9
9
 
10
10
  install_requires = \
11
11
  ['conf2levels>=0.4.0,<0.5.0',
12
- 'jflib>=1.0.0,<2.0.0',
13
- 'requests>=2.28.1,<3.0.0',
12
+ 'icinga2apic>=0.7.5,<0.8.0',
14
13
  'termcolor>=1.1.0,<2.0.0',
15
14
  'types-requests>=2.28.1,<3.0.0',
16
15
  'types-termcolor>=1.1.5,<2.0.0']
17
16
 
18
17
  setup_kwargs = {
19
18
  'name': 'command-watcher',
20
- 'version': '0.1.0',
19
+ 'version': '0.2.0',
21
20
  'description': 'Module to watch the execution of shell scripts. Both streams (`stdout` and `stderr`) are captured.',
22
21
  'long_description': "command_watcher\n===============\n\nModule to watch the execution of shell scripts. Both streams (`stdout`\nand `stderr`) are captured.\n\n.. code:: python\n\n watch = Watch()\n watch.log.critical('msg')\n watch.log.error('msg')\n watch.log.warning('msg')\n watch.log.info('msg')\n watch.log.debug('msg')\n watch.run(['rsync', '-av', '/home', '/backup'])\n\n.. code-block:: python\n\n from command_watcher import Watch\n watch = Watch(\n config_file='/etc/command-watcher.ini',\n service_name='texlive_update'\n )\n\n tlmgr = '/usr/local/texlive/bin/x86_64-linux/tlmgr'\n\n watch.run('{} update --self'.format(tlmgr))\n watch.run('{} update --all'.format(tlmgr))\n installed_packages = watch.run(\n '{} info --only-installed'.format(tlmgr), log=False\n )\n all_packages = watch.run('{} info'.format(tlmgr), log=False)\n\n watch.final_report(\n status=0,\n performance_data={\n 'installed_packages': installed_packages.line_count_stdout,\n 'all_packages': all_packages.line_count_stdout,\n },\n )\n\n.. code-block:: ini\n\n [email]\n subject_prefix = [cwatcher]\n from_addr =\n to_addr = logs@example.com\n to_addr_critical = critical@example.com\n smtp_login = mailer\n smtp_password = 1234\n smtp_server = mail.example.com:587\n\n [nsca]\n remote_host = 1.2.3.4\n password = asdf1234\n encryption_method = 8\n ; port = 5667\n\n [icinga]\n url = https://icinga.example.com:5665\n user = user\n password = 1234\n\n [beep]\n activated = True\n",
23
22
  'author': 'Josef Friedrich',
@@ -1,54 +0,0 @@
1
- import json
2
- from typing import Optional
3
-
4
- import requests
5
- import urllib3
6
-
7
- urllib3.disable_warnings() # type: ignore
8
-
9
-
10
- STATE_OK = 0
11
- STATE_WARNING = 1
12
- STATE_CRITICAL = 2
13
- STATE_UNKNOWN = 3
14
-
15
- States = {
16
- STATE_OK: 'OK',
17
- STATE_WARNING: 'WARNING',
18
- STATE_CRITICAL: 'CRITICAL',
19
- STATE_UNKNOWN: 'UNKNOWN',
20
- }
21
-
22
-
23
- def send_passive_check(url: str, user: str, password: str, status: int,
24
- host_name: str, service_name: str,
25
- text_output: str,
26
- performance_data: Optional[str] = None):
27
- """
28
- https://icinga.com/docs/icinga-2/latest/doc/12-icinga2-api/#process-check-result
29
-
30
- [icinga]
31
- url
32
- user
33
- password
34
- """
35
- request_url = '{}/v1/actions/process-check-result'.format(url)
36
- headers = {
37
- 'Accept': 'application/json',
38
- 'X-HTTP-Method-Override': 'POST'
39
- }
40
- data = {
41
- 'type': 'Service',
42
- 'filter': 'host.name=="{}" && service.name=="{}"'.format(host_name,
43
- service_name),
44
- 'exit_status': status,
45
- 'plugin_output': text_output,
46
- }
47
-
48
- if performance_data:
49
- data['performance_data'] = performance_data
50
-
51
- return requests.post(request_url,
52
- headers=headers,
53
- auth=(user, password),
54
- data=json.dumps(data), verify=False)
File without changes