Coverage for nurin/main.py: 91%

23 statements  

« prev     ^ index     » next       coverage.py v7.1.0, created at 2023-02-07 20:10 +0200

1from __future__ import annotations 

2 

3import logging 

4 

5import click 

6 

7from nurin.models import Config 

8from nurin.run import run 

9from nurin.__about__ import __version__ 

10 

11log = logging.getLogger(__name__) 

12 

13 

14@click.command( 

15 help="Network Up/Down monitor.\nTaasko se netti on nurin? Nurin se on!", 

16) 

17@click.option( 

18 "-p", 

19 "--ping-target", 

20 "ping_targets", 

21 multiple=True, 

22 default=["8.8.8.8"], 

23 help="Ping target; can be specified multiple times, and a random one is chosen each time.", 

24 metavar="IP/HOST", 

25) 

26@click.option( 

27 "--regular-check-interval", 

28 default=30, 

29 type=float, 

30 help="Regular check interval (seconds)", 

31 metavar="SECONDS", 

32) 

33@click.option( 

34 "--down-check-interval", 

35 default=5, 

36 type=float, 

37 help="Check interval when down counter > 0 (seconds)", 

38 metavar="SECONDS", 

39) 

40@click.option( 

41 "--sleep-jitter", 

42 default=0.1, 

43 type=float, 

44 help="Sleep jitter (multiplier, +/-)", 

45 metavar="MULTIPLIER", 

46) 

47@click.option( 

48 "--down-count", 

49 default=3, 

50 type=int, 

51 help="Number of consecutive failed pings before running down actions", 

52 metavar="NUM", 

53) 

54@click.option( 

55 "-da", 

56 "--down-action", 

57 "down_actions", 

58 multiple=True, 

59 default=["echo down"], 

60 help=( 

61 "Shell command to run when down counter > down-count; can be specified multiple times. " 

62 "All commands are run even if one fails." 

63 ), 

64) 

65@click.option( 

66 "--reset-after-down-action/--no-reset-after-down-action", 

67 default=False, 

68 help="Reset down count after running down actions?", 

69) 

70@click.option( 

71 "-ua", 

72 "--up-action", 

73 "up_actions", 

74 multiple=True, 

75 default=["echo up again"], 

76 help=( 

77 "Shell command to run when up again after down actions have been run; can be specified multiple times. " 

78 "All commands are run even if one fails." 

79 ), 

80) 

81@click.option("--max-cycles", default=None, type=int, help="Maximum number of cycles to run.") 

82@click.option("--debug", is_flag=True, help="Enable debug logging") 

83def main( 

84 *, 

85 debug: bool, 

86 down_actions: tuple[str, ...], 

87 down_check_interval: int, 

88 down_count: int, 

89 ping_targets: tuple[str, ...], 

90 regular_check_interval: int, 

91 reset_after_down_action: bool, 

92 sleep_jitter: float, 

93 up_actions: tuple[str, ...], 

94 max_cycles: int | None, 

95) -> None: 

96 logging.basicConfig( 

97 level=(logging.DEBUG if debug else logging.INFO), 

98 format="%(asctime)s %(levelname)s: %(message)s", 

99 ) 

100 config = Config( 

101 down_actions=list(down_actions), 

102 down_check_interval=down_check_interval, 

103 down_count=down_count, 

104 ping_targets=list(ping_targets), 

105 regular_check_interval=regular_check_interval, 

106 reset_after_down_action=reset_after_down_action, 

107 sleep_jitter=sleep_jitter, 

108 up_actions=list(up_actions), 

109 max_cycles=max_cycles, 

110 ) 

111 logging.info("Hello, this is nurin %s. My configuration is %s", __version__, config) 

112 run(config)