Skip to content

Commit

Permalink
Import some alert checks
Browse files Browse the repository at this point in the history
  • Loading branch information
trick77 committed Dec 29, 2023
1 parent 2fd08f9 commit 4338ff1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 36 deletions.
1 change: 0 additions & 1 deletion observium/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ RUN a2enmod rewrite php${PHP_VERSION} && \
ln -sf /dev/stderr /var/log/apache2/error.log

COPY bin/generate-config.php ./entrypoint.sh /usr/local/bin/

VOLUME /opt/observium/rrd
EXPOSE 8080

Expand Down
6 changes: 6 additions & 0 deletions observium/conf/observium/alert-checks.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
INSERT INTO `alert_tests` VALUES
(1,'storage','Storage exceeds 90% of disk','','{\"condition\":\"AND\",\"rules\":[{\"id\":\"device.hostname\",\"field\":\"device.hostname\",\"type\":\"string\n\",\"input\":\"text\",\"operator\":\"match\",\"value\":\"*\"}],\"valid\":true}','[{\"metric\":\"storage_perc\",\"condition\":\"ge\",\"value\":\"90\"}]',NULL,1,'crit',0,'',1,1,0,NULL),
(2,'mempool','Memory is above 97%','','{\"condition\":\"AND\",\"rules\":[{\"id\":\"device.hostname\",\"field\":\"device.hostname\",\"type\":\"string\",\"inpu\nt\":\"text\",\"operator\":\"match\",\"value\":\"*\"}],\"valid\":true}','[{\"metric\":\"mempool_perc\",\"condition\":\"ge\",\"value\":\"97\"}]',NULL,1,'warn',1,'',1,1,0,NULL),
(3,'processor','Processor is above 90%','','{\"condition\":\"AND\",\"rules\":[{\"id\":\"device.hostname\",\"field\":\"device.hostname\",\"type\":\"string\",\"input\":\"text\",\"operator\":\"match\",\"value\":\"*\"}],\"valid\":true}','[{\"metric\":\"processor_usage\",\"condition\":\"ge\",\"value\":\"90\"}]',NULL,1,'warn',1,'',1,1,0,NULL),
(5,'port','Port traffic above 95%','','{\"condition\":\"OR\",\"rules\":[{\"id\":\"device.hostname\",\"field\":\"device.hostname\",\"type\":\"string\",\"inpu\nt\":\"text\",\"operator\":\"match\",\"value\":\"*\"}],\"valid\":true}','[{\"metric\":\"ifInOctets_perc\",\"condition\":\"ge\",\"value\":\"95\"},{\"metric\":\n\"ifOutOctets_perc\",\"condition\":\"ge\",\"value\":\"95\"}]',NULL,1,'warn',2,'',1,1,0,NULL),
(6,'device','Device down!','','{\"condition\":\"AND\",\"rules\":[{\"id\":\"device.hostname\",\"field\":\"device.hostname\",\"type\":\"string\",\"\ninput\":\"text\",\"operator\":\"match\",\"value\":\"*\"}],\"valid\":true}','[{\"metric\":\"device_status\",\"condition\":\"equals\",\"value\":\"0\"}]',NULL,1,'crit',0,'',1,1,0,NULL);
85 changes: 50 additions & 35 deletions observium/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ set -o pipefail
set +e

if [ "${DEBUG_MODE,,}" == "true" ]; then
set -o xtrace
set -o xtrace
fi

db_hostname=db

show_observium_info() {
echo "****************************************************"
echo "* VERSION: $(cat ./VERSION)"
Expand All @@ -15,78 +17,91 @@ show_observium_info() {
echo "****************************************************"
}

check_db_connect() {
check_db_connection() {
attempt=0
rc=1
while [ $rc -ne 0 ]
do
let attempt++
echo "* Attempt ${attempt} trying to connect to database..."
mysql -h db -u ${DB_USER} --password=${DB_PASSWORD} -e "select 1" ${DB_NAME} >/dev/null
echo "Attempt ${attempt} trying to connect to database..."
mariadb --user ${DB_USER} -p${DB_PASSWORD} -D ${DB_NAME} -h ${db_hostname} -e "select 1" > /dev/null
rc=$?
[ $rc -ne 0 ] && sleep 1
done
echo "* Successfully connected to database"
echo "Successfully connected to database!"
}

set_dir_permissions() {
chown -R www-data:www-data ./rrd
chown -R www-data:www-data ./rrd
}

init_if_required() {
tables=`mysql -h db -u ${DB_USER} --password=${DB_PASSWORD} -e "show tables" ${DB_NAME} 2>/dev/null`
if [ -z "$tables" ]
tables=$(mariadb --user ${DB_USER} -p${DB_PASSWORD} -D ${DB_NAME} -h ${db_hostname} -e "show tables")
if [ -z "${tables}" ]
then
echo "* Database schema initialization required..."
echo "Database schema initialization required..."
./discovery.php -u
echo "* Creating admin user..."
echo "Creating admin user..."
./adduser.php "${OBSERVIUM_ADMIN_USER}" "${OBSERVIUM_ADMIN_PASSWORD}" 10
else
echo "* Database schema already initializied, no initialization required!"
echo "Database schema already initialized, no initialization required!"
fi
}

create_config() {
export OBSERVIUM__db_host='db'
export OBSERVIUM__db_name="${DB_NAME}"
export OBSERVIUM__db_user="${DB_USER}"
export OBSERVIUM__db_pass="${DB_PASSWORD}"
echo "$(php /usr/local/bin/generate-config.php)" > ./config.php
if [ "$SHOW_GENERATED_CONFIG_DURING_STARTUP" = "yes" ]; then
echo "* Created Observium's config.php with the following settings:"
cat ./config.php
fi
export OBSERVIUM__db_host='db'
export OBSERVIUM__db_name="${DB_NAME}"
export OBSERVIUM__db_user="${DB_USER}"
export OBSERVIUM__db_pass="${DB_PASSWORD}"
echo "$(php /usr/local/bin/generate-config.php)" > ./config.php
if [ "$SHOW_GENERATED_CONFIG_DURING_STARTUP" = "yes" ]; then
echo "Created Observium's config.php with the following settings:"
cat ./config.php
fi
}

import_snmp_devices() {
devices_file="/conf/snmp-devices.txt"
if [ -e "${devices_file}" ]; then
echo "Trying to import SNMP devices from ${devices_file}..."
while read -r params || [ -n "$params" ]; do
php ./add_device.php ${params} || true
done < <(grep -v "^#\|^$" "${devices_file}")
echo "Done importing SNMP devices!"
else
echo "Not importing any SNMP devices, file ${devices_file} does not exist!"
fi
}

import_devices() {
devices_file="/conf/devices.txt"
if [ -e "${devices_file}" ]; then
echo "* Trying to import devices from devices-import file ${devices_file}..."
while read -r params || [ -n "$params" ]; do
php ./add_device.php ${params} || true
done < <(grep -v "^#\|^$" "${devices_file}")
else
echo "* Devices-import file ${devices_file} does not exist, not importing any devices"
fi
import_alert_checks_if_required() {
count_alerts=$(mariadb --user ${DB_USER} -p${DB_PASSWORD} -D ${DB_NAME} -h ${db_hostname} -N -B -e "select count(*) from alert_tests")
if [ -z "$count_alerts" -o "$count_alerts" -eq 0 ]; then
echo "No alert checks found, importing some alert checks..."
mariadb --user ${DB_USER} -p${DB_PASSWORD} -D ${DB_NAME} -h ${db_hostname} < /conf/alert-checks.sql
echo "Successfully imported alert checks"
else
echo "Not importing alert checks, checks already exist."
fi
}

generate_smokeping_config() {
php ./generate-smokeping.php > /etc/smokeping/config.d/Targets
php ./scripts/generate-smokeping.php > /etc/smokeping/config.d/Targets
}

check_db_connect
check_db_connection
create_config
init_if_required
import_devices
import_snmp_devices
generate_smokeping_config
set_dir_permissions
import_alert_checks_if_required
show_observium_info

if [ "$1" != "" ]; then
echo "* Executing '$@'"
echo "Executing '$@'"
exec "$@"
elif [ -f "/usr/sbin/apache2ctl" ]; then
echo "* Starting httpd..."
echo "Starting httpd..."
exec apache2ctl -D FOREGROUND
else
echo "Unknown instructions. Exiting..."
Expand Down

0 comments on commit 4338ff1

Please sign in to comment.