-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabdata
More file actions
executable file
·104 lines (82 loc) · 3.13 KB
/
Copy pathabdata
File metadata and controls
executable file
·104 lines (82 loc) · 3.13 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
#!/usr/bin/env python3
import os
import subprocess
import click
from abackup import appcron
from abackup.data import Config
from abackup.data.check import perform_check
from abackup.data.updatecron import perform_update_cron
@click.group()
@click.pass_context
@click.option("--no-log", flag_value=True, help="Disable logging")
@click.option("--debug", flag_value=True, help="Enable debug-level of logging")
@click.option(
"--config",
type=click.Path(exists=True),
default=os.path.join(os.path.expanduser("~"), ".abackup/data.yml"),
help="Path to global configuration file, defaults to ~/.abackup/data.yml",
)
@click.option("--user", help="User to run cron commands for")
def cli(ctx, no_log: bool, debug: bool, config: str, user: str):
"""abdata - a script for monitoring disk health"""
config_path = config
config = Config(config_path, no_log, debug)
cron = appcron.AppCronTab("abdata", user, config.log)
abdata_options = []
if debug:
abdata_options.append("--debug")
if no_log:
abdata_options.append("--no-log")
if config_path != os.path.join(os.path.expanduser("~"), ".abackup/data.yml"):
abdata_options.extend(["--config", config_path])
if user:
abdata_options.extend(["--user", user])
ctx.obj = {"config": config, "cron": cron, "log": config.log, "abdata_options": " ".join(abdata_options)}
@cli.command("log")
@click.pass_context
def log_command(ctx):
"""Display the log on the terminal"""
subprocess.run(["less", "+G", ctx.obj["config"].log_path])
@cli.command("update-cron")
@click.pass_context
def update_cron_command(ctx):
"""Update crontab with config settings
This will add or update the crontab entry for the pools to monitor.
"""
config = ctx.obj["config"]
cron = ctx.obj["cron"]
log = ctx.obj["log"]
abdata_options = ctx.obj["abdata_options"]
log.info("--- Updating crontab")
ret = perform_update_cron(config.drivers, abdata_options, cron, log)
if ret:
log.info("--- Crontab updated.")
else:
log.critical("--- Crontab not updated!")
exit(1)
@cli.command("check")
@click.pass_context
@click.option(
"--driver", type=click.Choice(["mdadm", "zfs"]), help="Only perform the " "check on the specified driver."
)
@click.option("--pool", help="Limit the check to just this pool.")
@click.option(
"--notify",
type=click.Choice(["auto", "always", "never"]),
default="never",
help="Notification setting, auto: notify on failure, always: always notify, never: never notify. "
"Defaults to never.",
)
@click.option("--healthchecks", flag_value=True, help="Perform healthchecks if configured.")
def check_command(ctx, driver: str, pool: str, notify: str, healthchecks: bool):
"""Perform a check the configured pools.
This will check the health of the disk pools.
"""
config = ctx.obj["config"]
log = ctx.obj["log"]
log.info("--- Checking")
drivers = [config.drivers[driver]] if driver else config.drivers.values()
perform_check(config, drivers, notify, log, pool, do_healthchecks=healthchecks)
log.info("--- Finished check")
if __name__ == "__main__":
cli()