Let's make the world a better place using digital technologies!

Day: February 18, 2018

Simple URL Monitor and Mobile Push Notifications using Pushover

Often we would like to monitor the online status of one or more websites or urls in order to be able to fix any issues as soon as a problem arises. I created the following simple mechanism to probe a list of url’s and send push notifications in case of an error.

#!/usr/bin/env python3

# Script to check url health and send push notifications using pushover servrice
# Author: Dr. Asif Rana (aiqbalrana@gmail.com)
# License: MIT License 
# Date: 20180213, 12:06 CET

import urllib.request
import http.client, urllib
import sys
sitelist = {
  'http://site1url/' : 'site1 label',
  'http://site2url/' : 'site2 label', }
for x in sitelist:
  statuslive = 0
  try:
    url = urllib.request.urlopen(x)
    code = url.getcode()
    if (code == 200):
      statuslive = 1
  except:
    statuslive = 0
 
  if (statuslive == 0):
    conn = http.client.HTTPSConnection("api.pushover.net:443")
    conn.request("POST", "/1/messages.json", urllib.parse.urlencode({
    "token": "your pushover.net application token",
    "user": "your pushover.net user key", "
    message": "Site down: " + sitelist[x], }), { "Content-type": "application/x-www-form-urlencoded" })
    conn.getresponse()

Ideally, you would run this script on a different server than the site you would like to monitor. A pushover.net account will be needed to get your token and key. In addition, you would need to install the pushover app on your iOS or Android device to get the notifications.

Tested and works great for a simple URL monitoring and getting notifications about website crashes, etc.!

You need to add this script as a cronjob using crontab tool and run it on regular intervals (e.g., every 5 minutes) to check the urls.

Continue Reading

Managing Repeat Offender in OSSEC

In ossec.conf:

<active-response>
    <repeated_offenders>60,120,1440</repeated_offenders>
 </active-response>

where 60, 120, 1440 indicate minutes banned after first, second, and third offense. Adapt these values to your taste.

Continue Reading

Create REST Service on Raspberry Pi with Flask

Creating the REST script using Flask

  • First install Flask on raspi:
pip3 install flask
  • Create a REST service using the following code. This is a very simple service that provides one command. In addition, I’ve created another interface also for providing psutil functions, as a demo.
# Script to provide a rest interface to raspberry 
# Author: Dr. Asif Rana (aiqbalrana@gmail.com) 
# License: MIT License
# Date: 20180217 
 
from flask import Flask, url_for 
import os 
import sys 
import psutil 
 
raspicmds = { 
 'reboot' : '/sbin/reboot' 
 } 
 
app = Flask(__name__) 
 
@app.route('/osinfo/<cmd>', methods=['GET', 'POST']) 
def api_osinfo(cmd): 
 cmdstr = 'psutil.' + cmd 
 cmdresp = eval(cmdstr) 
 return str(cmdresp) 
 
@app.route('/raspi/<kcmd>', methods=['GET', 'POST']) 
def api_raspicmd(kcmd): 
 if kcmd in raspicmds: 
 os.system("sudo " + raspicmds[kcmd]) 
 return str(kcmd + 'executed') 
 
if __name__ == '__main__': 
 app.run(debug=True, host='0.0.0.0') 
  • Caution: do not expose your raspi to the internet without taking good care of security. This example is just for illustration purposes.
  • Making the script a service (daemon)

  • Create this file: /lib/systemd/system/raspirest.service with following contents:
[Unit]
Description=Raspi REST Interface
[Service]
Type=simple
ExecStart=/usr/src/scripts/raspirest.py
[Install]
WantedBy=multi-user.target
  • Add and enable the service to start at boot:
sudo systemctl daemon-reload
sudo systemctl enable raspirest
  • Check if the service is running by calling it at: http://IP:Port/raspi/<yourcmd>
Continue Reading