Skip to content

Configuration

Module for reading configuration.

This module provides a function for reading a JSON file into the provided Settings objects.

Generic

Generic method for reading dictionary values.

Source code in pi_monitor/configuration.py
29
30
31
32
33
34
35
36
class Generic:
    """Generic method for reading dictionary values."""

    @classmethod
    def from_dict(cls, dict):
        obj = cls()
        obj.__dict__.update(dict)
        return obj

HealthCheckSettings

Settings for a HealthCheck.

A Healthcheck represents a simple request to the defined url. If a non-200 the request generates an exception or a non-200 response, the site is determined to be down.

If status_page is defined, statuspage.io will be updated according to the following rules.

  • If the site returns a 2xx response and statuspage.io lists the component as non-operational:
    • The component's status will be set to operational
    • Any open incidents associated with this component will be marked as resolved
  • If the site returns a non-2xx response or an exception and statuspage.io lists the component as operational:
    • The component's status will be set to operational
    • An incident will be opened using the name and associated with this component.

Attributes:

Name Type Description
name str

The name of the site being checked

url str

The url to be fetched as part of the check

status_page StatusPageComponentSettings

Any StatusPage-related component settings

Source code in pi_monitor/configuration.py
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
class HealthCheckSettings:
    """Settings for a HealthCheck.

    A Healthcheck represents a simple request to the defined `url`.
     If a non-200 the request generates an exception or a non-200
     response, the site is determined to be down.

    If `status_page` is defined, statuspage.io will be updated
     according to the following rules.

    - If the site returns a 2xx response and statuspage.io lists
     the component as non-operational:
        - The component's status will be set to operational
        - Any open incidents associated with this component will
         be marked as resolved
    - If the site returns a non-2xx response or an exception and
     statuspage.io lists the component as operational:
        - The component's status will be set to operational
        - An incident will be opened using the `name` and
         associated with this component.


    Attributes:
        name (str): The name of the site being checked
        url (str): The url to be fetched as part of the check
        status_page (StatusPageComponentSettings): Any StatusPage-related
            component settings
    """

    name: str
    url: str
    status_page: StatusPageComponentSettings

MonitorSettings

MonitorSettings

This class represents the entire structure of the configuration file (monitor.config.json by default).

Attributes:

Name Type Description
status_checks List[HealthCheckSettings]

The collection of statusCheck settings

notification NotificationSettings

The settings object for notifications

status_page StatusPageSettings

The settings object for StatusPage.io

Source code in pi_monitor/configuration.py
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
class MonitorSettings:
    """MonitorSettings

    This class represents the entire structure of the configuration
    file (`monitor.config.json` by default).

    Attributes:
        status_checks: The collection of statusCheck settings
        notification: The settings object for notifications
        status_page: The settings object for StatusPage.io
    """

    status_checks: List[HealthCheckSettings]
    notification: NotificationSettings
    status_page: StatusPageSettings

NotificationSettings

Notification Settings

This class represents settings for notifications. If you are using Gmail to send, you need to set your account's Allow Less Secure Apps setting to true

Attributes:

Name Type Description
smtp_url str

The URL of the SMTP host

smtp_port int

The SMTP Port to use

smtp_sender_id str

The SMTP user

smtp_sender_apikey str

The SMTP user's password

sms_email str

The email to receive notifications

Source code in pi_monitor/configuration.py
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
class NotificationSettings:
    """Notification Settings

    This class represents settings for notifications.  If you are using Gmail to send,
    you need to set your account's `Allow Less Secure Apps` setting to `true`

    Attributes:
        smtp_url (str): The URL of the SMTP host
        smtp_port (int): The SMTP Port to use
        smtp_sender_id (str): The SMTP user
        smtp_sender_apikey (str): The SMTP user's password
        sms_email: The email to receive notifications

    """

    smtp_url: str
    smtp_port: int
    smtp_sender_id: str
    smtp_sender_apikey: str
    sms_email: str

StatusPageComponentSettings

Settings for StatusPage.io components.

Attributes:

Name Type Description
component_id str

The ID of the component in your statuspage.io page

Source code in pi_monitor/configuration.py
19
20
21
22
23
24
25
26
class StatusPageComponentSettings:
    """Settings for StatusPage.io components.

    Attributes:
        component_id (str): The ID of the component in your statuspage.io page
    """

    component_id: str

StatusPageSettings

Settings for StatusPage.io.

Attributes:

Name Type Description
api_key str

The API Key to access statuspage.io

page_id str

Your PageId for statuspage.io

Source code in pi_monitor/configuration.py
73
74
75
76
77
78
79
80
81
82
class StatusPageSettings:
    """Settings for StatusPage.io.

    Attributes:
        api_key (str): The API Key to access statuspage.io
        page_id (str): Your PageId for statuspage.io
    """

    api_key: str
    page_id: str

read_configuration(file, default_settings={})

Read Configuration file and return settings

Parameters:

Name Type Description Default
file str

The file name to use for configuration. The default value is monitor.config.json

required
default_settings MonitorSettings

A default instance of the settings to use if the file cannot be found.

{}

Returns:

Name Type Description
MonitorSettings MonitorSettings

A MonitorSettings object populated from the given file, or an empty Settings object.

Source code in pi_monitor/configuration.py
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
def read_configuration(
    file: str, default_settings: MonitorSettings = {}
) -> MonitorSettings:
    """Read Configuration file and return settings

    Args:
        file: The file name to use for configuration.
                The default value is `monitor.config.json`
        default_settings: A default instance of the settings to use if the file
                            cannot be found.

    Returns:
        MonitorSettings: A MonitorSettings object populated from the given file,
                        or an empty Settings object.
    """
    config_path = Path(file)

    if not config_path.exists():
        logger.error("Configuration file not found: %s.  Using default", file)
        return default_settings

    config_data = config_path.read_text()
    return json.loads(config_data, object_hook=Generic.from_dict)