1 | import json |
||
2 | import os |
||
3 | from pathlib import Path |
||
4 | from typing import Dict, Optional |
||
5 | |||
6 | from cleo import Command |
||
7 | |||
8 | from backuppc_clone.Config import Config |
||
9 | |||
10 | |||
11 | View Code Duplication | class NagiosCommand(Command): |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
12 | """ |
||
13 | Performance a status check for Nagios |
||
14 | |||
15 | nagios |
||
16 | {clone.cfg : The configuration file of the clone} |
||
17 | """ |
||
18 | |||
19 | # ------------------------------------------------------------------------------------------------------------------ |
||
20 | def __print_status(self, status: str, message: str, perf_data: Optional[str] = None) -> None: |
||
21 | """ |
||
22 | Prints the status of BackupPC Clone. |
||
23 | """ |
||
24 | if perf_data: |
||
25 | print('BackupPC Clone {} - {} | {}'.format(status, message, perf_data)) |
||
26 | else: |
||
27 | print('BackupPC Clone {} - {}'.format(status, message, perf_data)) |
||
28 | |||
29 | # ------------------------------------------------------------------------------------------------------------------ |
||
30 | def __get_performance_data(self, stats: Dict[str, any]) -> str: |
||
31 | """ |
||
32 | Returns the performance data string. |
||
33 | |||
34 | @param dict stats: The stats of BackupPC Clone. |
||
35 | |||
36 | :rtype: str |
||
37 | """ |
||
38 | return 'backups={} cloned_backups={} not_cloned_backups={} obsolete_cloned_backups={}' \ |
||
39 | .format(stats['n_backups'], |
||
40 | stats['n_cloned_backups'], |
||
41 | stats['n_not_cloned_backups'], |
||
42 | stats['n_obsolete_cloned_backups']) |
||
43 | |||
44 | # ------------------------------------------------------------------------------------------------------------------ |
||
45 | def handle(self) -> int: |
||
46 | """ |
||
47 | Executes the command. |
||
48 | """ |
||
49 | config_filename_clone = self.input.get_argument('clone.cfg') |
||
50 | if os.path.exists(config_filename_clone): |
||
51 | config = Config(config_filename_clone) |
||
52 | text = Path(config.stats_file).read_text() |
||
53 | stats = json.loads(text) |
||
54 | |||
55 | status = 'OK' |
||
56 | message = 'Clone up to date' |
||
57 | perf_data = self.__get_performance_data(stats) |
||
58 | exit_status = 0 |
||
59 | else: |
||
60 | status = 'ERROR' |
||
61 | message = 'Clone not found' |
||
62 | perf_data = None |
||
63 | exit_status = 2 |
||
64 | |||
65 | self.__print_status(status, message, perf_data) |
||
66 | |||
67 | return exit_status |
||
68 | |||
69 | # ---------------------------------------------------------------------------------------------------------------------- |
||
70 |