Skip to content

Tutorial SensorClientPing

sqall01 edited this page Jul 27, 2016 · 21 revisions

Tutorial - alertR Sensor Client Ping

In this document a tutorial for the version 0.400 is given.

Table of Contents

Tutorial

# Tutorial

This tutorial describes the basic configuration of the alertR Sensor Client Ping. It can be used as foundation to develop an own alertR system setup. This tutorial assumes you have set up the alertR Server according to the provided Server Tutorial.

## Installation

The alertR Sensor Client Ping does not have special prerequisites. Therefore, we can install it directly.

It is always a good idea to have an own user for a daemon. Therefore, we create a user that runs the alertR Sensor Client Ping called "alertr":

root@towel:/home/sqall# adduser --disabled-password alertr
Adding user `alertr' ...
Adding new group `alertr' (1002) ...
Adding new user `alertr' (1002) with group `alertr' ...
Creating home directory `/home/alertr' ...
Copying files from `/etc/skel' ...
Changing the user information for alertr
Enter the new value, or press ENTER for the default
    Full Name []:
    Room Number []:
    Work Phone []:
    Home Phone []:
    Other []:
Is the information correct? [Y/n] y

To switch into this user in the terminal you can use the following command:

root@towel:/home/sqall# su alertr

Please note that this is only possible as root user. If you are trying this command as another user you will get a password prompt for the user alertr, which does not have a password.

Afterwards you can use the installation script provided by alertR to install the client.

alertr@towel:~$ wget https://raw.githubusercontent.com/sqall01/alertR/master/alertRinstaller.py

alertr@towel:~$ mkdir sensorClientPing

alertr@towel:~$ python alertRinstaller.py -i sensorClientPing -t ./sensorClientPing/
## Configuration

The following describes the configuration of the alertR Sensor Client Ping. It shows a basic configuration that can be used as a template for own installations.

### Certificate

For security reasons, the alertR system enforces a TLS connection. During the installation of the alertR server, you created a certificate file with the name alertr_server.crt. This file is needed by the client in order to verify the connection. Please copy it to the host you are installing this client on. This tutorial assumes that you have stored the certificate file in the following location: /home/alertr/sensorClientPing/config/alertr_server.crt.

### User Credentials

The client has to authenticate itself before it can work with the server. Therefore, we need to set up user credentials on the alertR server for this client. We add the following line to the server user credentials file /home/alertr/server/config/users.csv (which was set up during the server installation.):

user_ping, password_ping, sensor, sensorClientPing

Remember to restart the alertR server in order to activate the user credentials.

### AlertLevels

This tutorial assumes the alert levels configured in the alertR Server tutorial. The client will use the following for its configuration:

AlertLevel Name Functionality
1 Silent Notification Used to silently notify the user.

AlertLevel 1 is used for sensors that trigger a sensorAlert, but not an urgent one. Since this client checks hosts with a ping request, we only want a normal notification if something is wrong as we assume it is not as urgent if a host is down.

### Configuration File

Every alertR installation has a template configuration file with detailed comments that describe the functionality of each option. To use it as a draft, you can just copy the template file and modify your new configuration file.

alertr@towel:~/sensorClientPing/config$ cp config.xml.template config.xml

The configuration file itself is split into the following parts:

  • General
  • SMTP
  • Update
  • Sensors

In this tutorial, we are going through each of these parts separately and describe why we configured it like that.

#### General

The general section is used for options such as certificate file location. In our tutorial configuration, the section looks like this:

	<general>

		<log
			file="/home/alertr/sensorClientPing/config/logfile.log"
			level="INFO" />

		<server
			host="localhost"
			port="44556"
			caFile="/home/alertr/sensorClientPing/config/alertr_server.crt" />

		<client
			certificateRequired="False"
			certFile="/absolute/path/to/client.crt"
			keyFile="/absolute/path/to/client.key" />

		<credentials
			username="user_ping"
			password="password_ping" />

	</general>

Section log is used to configure the log file setup. The file attribute sets the location for the log file. Please note that this has to be the absolute path to the location. Make sure it exists and is writable by the user that starts the alertR client. Otherwise the client will not start. For example, the directory /var/log is usually only writable by the "root" user and users of the group "syslog". If you want the client log file in this directory, please create a sub-directory for the alertR client log file and set the correct permissions on it. The level attribute sets the log level. Possible values are DEBUG, INFO, WARNING, ERROR, CRITICAL.

Section server is used to configure the server connection setup. The host and port attribute sets the address and port of the alertR server. The port was previously configured in the server configuration file. The address can either be a name that can be resolved by the DNS system or an IP address. The caFile attribute sets the location of the certificate file that is used to authenticate the server (see Certificate). Please make sure that the location is an absolute path to the file.

Section client is used to configure the client connection setup. The certificateRequired attribute determines if the client needs a certificate to connect to the server or not. The certFile and keyFile are used to determine the client certificate and key file. Since this tutorial is not using client certificates, we do not have to set sane values here.

Section credentials is used to configure the login credentials of this client. The credentials were configured in a previous step of this Tutorial and must be the same as in this configuration file.

#### SMTP

The smtp section is used for options that configure the used SMTP server. In our tutorial configuration, the section looks like this:

	<smtp>

		<general
			activated="True"
			fromAddr="sensorClientPing@alertr.de"
			toAddr="myalarmaddress@example.org" />

		<server
			host="127.0.0.1"
			port="25" />

	</smtp>

Section general is used to configure the basic options of the eMail notification. The eMail notification is used if problems on the alertR client occur that can not be solved (like connection problems). The activated attribute determines if the eMail notification is activated or not. If it is deactivated, the rest of the options in the whole smtp section are ignored. The fromAddr attribute sets the used eMail address from which the eMail notification is sent. The toAddr attribute determines the eMail address the notification is sent to.

Section server is used to configure the SMTP server that is used to send the eMail notification. The host attribute sets the address and the port attribute sets the port of the used SMTP server. At the moment, only "127.0.0.1" is allowed as address and port 25. This means you have to set up a local SMTP server in order to send eMail notifications. Personally, I would suggest to set up a local Postfix SMTP server that can forward eMails like in this Tutorial. Please note that the local SMTP server has to accept eMails from localhost without any authentication since it is not supported by alertR yet.

#### Update

The update section is used for options such as update checking. In our tutorial configuration, the section looks like this:

	<update>

		<general
			activated="True"
			interval="86400"
			emailNotification="True" />

		<server
			host="raw.githubusercontent.com"
			port="443"
			location="/sqall01/alertR/master/"
			caFile="/etc/ssl/certs/DigiCert_High_Assurance_EV_Root_CA.pem" />

	</update>

Section general is used to configure the update checking setup. The activated attribute defines if the update checking should be activated. If it is activated, the alertR instance will check regularly the Internet repository if a new version is available. The interval gives the seconds the alertR instance waits before checking for updates. The emailNotification attribute determines if an eMail notification is sent when a new update is available. This only works, if the SMTP settings are configured.

Section server is used to configure the remote update repository. Normally, the default configuration can be used here (which is this repository). The host attribute gives the remote server location of the repository. The port attribute gives the port on which the repository is reachable (alertR uses HTTPS for it). The location attribute sets the location on the remote repository. The caFile attribute gives the local location of the certificate that is used to verify the HTTPS connection. The default value is the location on Debain/Ubuntu distributions of the current certificate that is used by Github.

#### Sensors

The sensors section configures the sensors of this client. In our tutorial configuration, the section looks like this:

	<sensors>

		<sensor>

			<general
				id="0"
				description="alertr.de reachable"
				alertDelay="0"
				triggerAlert="True"
				triggerAlertNormal="True"
				triggerState="1" />

			<alertLevel>1</alertLevel>

			<ping
				host="alertr.de"
				execute="/bin/ping"
				timeout="30"
				intervalToCheck="60" />

		</sensor>

		<sensor>

			<general
				id="1"
				description="router reachable"
				alertDelay="0"
				triggerAlert="True"
				triggerAlertNormal="True"
				triggerState="1" />

			<alertLevel>1</alertLevel>

			<ping
				host="192.168.2.1"
				execute="/bin/ping"
				timeout="30"
				intervalToCheck="60" />

		</sensor>

	</sensors>

Each configured sensor is set up in its own <sensor>...</sensor> context. The context has three main sections: general, alertLevel and ping. The following describes the settings of each section.

Section general is used to configure the basic sensor settings. The id attribute gives the local id of the configured sensor. This id has to be unique for each sensor of this client. Since we configure two sensors, we give them the id 0 and 1. The description attribute gives a short description of the sensor. The alertDelay attribute sets how long in seconds the server has to wait until a sensorAlert is triggered. Normally, this is set to 0 to immediate react on a sensorAlert. But in some situations it might be necessary to react on a sensorAlert after a certain time and not directly. For our tutorial sensors we set this value to 0. The triggerAlert attribute sets if the sensor triggers a sensorAlert with the state "triggered" and triggerAlertNormal sets if the sensor triggers a sensorAlert with the state "normal". Both our sensors should trigger both sensorAlerts. The triggerState state configures which internal state is used to trigger a sensorAlert. In a normal configuration, this is 1.

The alertLevel sections set the alertLevel the sensor triggers. In our tutorial, both sensors trigger only alertLevel 1. But it is not limited to only one alertLevel. If another alertLevel should be triggered, an additional alertLevel section is added with the corresponding alertLevel.

Section ping configures the ping sensor settings. The host attribute sets the host that is pinged by the sensor. In our tutorial, one sensor checks if the alertr.de server is reachable and the other if the router with the IP address 192.168.2.1 is reachable. The execute attributes sets where the ping command can be found. On a Linux distribution, this is normally /bin/ping. The timeout attribute sets the time in seconds which the sensor waits until it regards the ping command as timed out and triggers a sensorAlert. The intervalToCheck attributes sets the time in seconds which the sensor waits before it issues another ping request.

### Autostart

If you want the alertR client to start automatically after a reboot of the host, you have to set it up. In this tutorial, I will give two examples on how to set up the autostart of the alertR client depending on your system using systemd or just init.d.

#### Init.d

An init.d script is provided by the alertR installation. The only thing you have to do is to copy and configure it correctly. The following command copies it to the correct location, sets the permissions correctly and installs it:

root@towel:/home/alertr/sensorClientPing/init.d_example# cp alertRsensorPing.sh /etc/init.d/

root@towel:/etc/init.d# chown root:root alertRsensorPing.sh
root@towel:/etc/init.d# chmod 755 alertRsensorPing.sh

root@towel:/etc/init.d# update-rc.d alertRsensorPing.sh defaults
 Adding system startup for /etc/init.d/alertRsensorPing.sh ...
   /etc/rc0.d/K20alertRsensorPing.sh -> ../init.d/alertRsensorPing.sh
   /etc/rc1.d/K20alertRsensorPing.sh -> ../init.d/alertRsensorPing.sh
   /etc/rc6.d/K20alertRsensorPing.sh -> ../init.d/alertRsensorPing.sh
   /etc/rc2.d/S20alertRsensorPing.sh -> ../init.d/alertRsensorPing.sh
   /etc/rc3.d/S20alertRsensorPing.sh -> ../init.d/alertRsensorPing.sh
   /etc/rc4.d/S20alertRsensorPing.sh -> ../init.d/alertRsensorPing.sh
   /etc/rc5.d/S20alertRsensorPing.sh -> ../init.d/alertRsensorPing.sh

Next we have to configure the init.d script correctly. The following changes have to be made to the script:

[...]
# change USER to the user which runs the alertRclient
USER=alertr
# change DAEMON to the path to run the alertRclient
DAEMON=/home/alertr/sensorClientPing/alertRclient.py
[...]

In order to work correctly, the alertR client has to be executable. With the following, we change the permissions of the alertR client:

alertr@towel:~/sensorClientPing$ chmod 755 alertRclient.py

To start the client as daemon, we can now execute:

root@towel:/home/alertr/sensorClientPing# /etc/init.d/alertRsensorPing.sh start

The log file of the client (it is created in the directory you have configured in the General Section) should now show the following:

[...]
07/23/2016 10:26:16 INFO: [alertRclient.py] Initializing sensors.
07/23/2016 10:26:16 INFO: [alertRclient.py] Connecting to server.
07/23/2016 10:26:16 INFO: [alertRclient.py] Starting watchdog thread.
07/23/2016 10:26:16 INFO: [alertRclient.py] Starting update check thread.
07/23/2016 10:26:16 INFO: [alertRclient.py] Client started.
#### Systemd

If your system uses systemd for its autostart, you have to configure the alertR client a bit different. Copying the template and setting permissions is still the same:

root@towel:/home/alertr/sensorClientPing/init.d_example# cp alertRsensorPing.sh /etc/init.d/

root@towel:/etc/init.d# chown root:root alertRsensorPing.sh
root@towel:/etc/init.d# chmod 755 alertRsensorPing.sh

Configuring the init.d script correctly is also the same:

[...]
# change USER to the user which runs the alertRclient
USER=alertr
# change DAEMON to the path to run the alertRclient
DAEMON=/home/alertr/sensorClientPing/alertRclient.py
[...]

Now, systemd has to be configured. For this we copy the template service file to the correct location and install the service:

root@towel:/home/alertr/sensorClientPing/init.d_example# cp alertRsensorPing.service /etc/systemd/system/

root@towel:/home/alertr/sensorClientPing# systemctl enable alertRsensorPing.service

The alertR client can be started with the following command:

root@towel:/home/alertr/sensorClientPing# service alertRsensorPing.sh status

The log file of the client (it is created in the directory you have configured in the General Section) should now show the following:

[...]
07/23/2016 10:26:16 INFO: [alertRclient.py] Initializing sensors.
07/23/2016 10:26:16 INFO: [alertRclient.py] Connecting to server.
07/23/2016 10:26:16 INFO: [alertRclient.py] Starting watchdog thread.
07/23/2016 10:26:16 INFO: [alertRclient.py] Starting update check thread.
07/23/2016 10:26:16 INFO: [alertRclient.py] Client started.
## Troubleshooting

If you experience problems, please check the log file first. If it is not helpful, change the log level to DEBUG and check again. If no error can be seen, please start the alertR client manually and check if an error occurs that is not printed into the log file. This can be done by just executing the alertR client as the user that it normally runs with.

alertr@towel:~/sensorClientPing$ ./alertRclient.py

If you still have problems and do not know how to solve them, you can use the Github Issues.

Clone this wiki locally