{"id":13689109,"url":"https://github.com/naztronaut/easyUptime","last_synced_at":"2025-05-01T23:32:16.198Z","repository":{"id":82253291,"uuid":"185077165","full_name":"naztronaut/easyUptime","owner":"naztronaut","description":"Implementation of Uptime monitor without a Raspberry Pi","archived":false,"fork":false,"pushed_at":"2020-07-04T22:01:47.000Z","size":22,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-08-03T15:15:24.817Z","etag":null,"topics":["cron-jobs","flask","gpio-pins","python","raspberry-pi","uptime-monitor","website-uptime"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/naztronaut.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null},"funding":{"github":null,"patreon":"nazmus","open_collective":null,"ko_fi":"nazmus","tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2019-05-05T20:08:06.000Z","updated_at":"2021-05-05T06:29:10.000Z","dependencies_parsed_at":null,"dependency_job_id":"860b76fb-9551-4e55-8c46-49ff79f3ebca","html_url":"https://github.com/naztronaut/easyUptime","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/naztronaut%2FeasyUptime","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/naztronaut%2FeasyUptime/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/naztronaut%2FeasyUptime/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/naztronaut%2FeasyUptime/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/naztronaut","download_url":"https://codeload.github.com/naztronaut/easyUptime/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224282125,"owners_count":17285775,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cron-jobs","flask","gpio-pins","python","raspberry-pi","uptime-monitor","website-uptime"],"created_at":"2024-08-02T15:01:33.846Z","updated_at":"2024-11-12T13:30:47.546Z","avatar_url":"https://github.com/naztronaut.png","language":"Python","funding_links":["https://patreon.com/nazmus","https://ko-fi.com/nazmus"],"categories":["flask"],"sub_categories":[],"readme":"# Easy Uptime Monitor\n\nThis is a newer implementation of the Raspberry Pi Website Uptime Monitor. This app can be installed on any Linux system, not just Raspberry Pi. \n\nThere are also no physical LEDs associated with this, but virtual ones remain for a GUI that's in the works. \n\nEverything below needs to be updated! \n\n----\n\n## Demo\n\u003ca href=\"https://www.youtube.com/watch?v=CBtRuy5vFhI\" target=\"_blank\"\u003e\u003cimg src=\"https://www.easyprogramming.net/img/uptimeMonitor.jpg\" width=\"700\" /\u003e\u003c/a\u003e\n###### Click image to watch demo on YouTube\n\n## Table of Contents\n\n1. [Getting Started](#getting-started)\n    1. [Hardware](#hardware)\n    2. [RPi Pin Usage](#rpi-pin-usage)\n2. [Installation](#installation)\n    1. [Website List](#website-list-upjson)\n3. [Configuration](#configuration)\n    1. [LED Pins](#led-pins)\n    2. [Email Config](#email-config)\n    3. [Database Config](#database-config)\n    4. [Virtual Environment](#virtual-environment--dependencies)\n4. [Cron Jobs](#initialize-cron-jobs)\n5. [Flask](#flask)\n    1. [Run Flask App](#run-flask-app)\n    2. [Web Service Endpoints](#web-service-endpoints)\n6. [Apache](#apache)\n7. [Backlog Items](#backlog-items)\n8. [Authors](#authors)\n9. [License](#license)\n    \n\n## Getting Started\n\nThese instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.\n\nDemo Pic of project in progress:\n\n\u003cimg src=\"https://i.imgur.com/yYHUCZ2.jpg\" alt=\"Website Uptime Monitor\" width=\"700\" /\u003e\n\n### Hardware\n\n- Raspberry Pi - I use Zero W for this project\n- Red Dome LED + Resistor\n- Yellow Dome LED + Resistor\n- Green Dome LED + Resistor\n- Optional protoboard and pins to make a hat for the Pi\n\n\n### RPi pin usage\n\nThe following pin settings were used for this project:\n\n```python\ngreen = 12\nyellow = 25\nred = 18\n```\n\nThe pins can be modified in the [LED Pins Configuration](#led-pins) section below\n\n## Installation\n\n**NOTE**: _This guide assumes that you have MySQL installed with a database named `uptime` - you can change the database name and user in the configurations below._  \n\nIf you haven't already, You can either clone this repository or download the latest release. To clone the repo run this command:\n```bash\ngit clone https://github.com/naztronaut/RaspberryPi-Website-Uptime-Monitor.git\n```\n\nTo get the latest release, check out the Latest Releases: https://github.com/naztronaut/RaspberryPi-Website-Uptime-Monitor/releases/latest\n\nAt the time of this update, the latest version is version `Beta 0.3.0-b01`. To get this directly, run the following commands:\n\n```bash\nwget https://github.com/naztronaut/RaspberryPi-Website-Uptime-Monitor/archive/0.3.0-b01.zip\n```\n\nThen unzip it with:\n\n```bash\nunzip 0.3.0-b01.zip\n```\n\nFor simplicity's sake, I recommend changing the name of the directory/repo to `uptime` as I've done it in and will be using for the rest of this guide. You can do so easily with this command:\n\n```bash\nmv RaspberryPi-Website-Uptime-Monitor-0.3.0-b01 uptime\n```\n\nUpdate the directory name as needed. \n\n### Website List in `sites` table\n\nDependency on `up.json` was removed as of version `0.5.0`. Sites are now stored in the database inside the `sites` table.  \n\nThis application will check to see if a `200` is returned from the website and it will only check websites that are currently active and visible (not deleted). \nThe previously used `up.json` didn't do too much in addition to checking for a status of `200` so it was removed. I am working on ideas on how to check the actual content\nfor a better sense of website uptime. \n\n## Configuration\n\nThe below configurations are located in `config/config.sample.py` - before continuing, rename the file to `config.py` and adjust the values below. There are two main configuration categorie: Database and Email. \n\nTo rename the file, run this command:\n\n```bash\nmv config.sample.py config.py\n```\n\n### LED Pins\n\nThe GPIO pins for the LEDs that you will install can be modified in the conf file. Edit the `LED_PINS` object as you see fit. By default, the pins listed above are used:\n\n```python\nLED_PINS = {\n    'green':    12,\n    'yellow':   25,\n    'red':      18\n}\n```\n\n### Email Config\n\nIf you want to use the email functionality, edit the `EMAIL_CONFIG` object in `config.py` and enter your username and password. The Mail server and Port are also configurable. \nBy default, this app uses Gmail as the mail server and port 465 for SSL. Feel free to change the values to your own specs. Recommended to keep port as the SSL port. And finally, \nedit the sender with your email address and recipient as whoever wants to receive the notification. This app currently only allows one recipient. \n\n```python\nEMAIL_CONFIG = {\n    'username': '\u003cUSERNAME\u003e',\n    'password': '\u003cPASSWORD\u003e',\n    'smtpServer': 'smtp.gmail.com',\n    'port': 465,\n    'sender': 'Email of who will send it',\n    'recipient': 'Email of who will receive it'\n}\n```  \n\nOnce you've made the edits, move onto the database config. \n\n### Database Config\n\nBefore creating the schema, edit `config/config.py` and update the `DATABASE_CONFIG` configurations with the database that you'll create in the next step:\n\n```python\nDATABASE_CONFIG = {\n    'host' : 'localhost',\n    'dbname' : 'uptime',\n    'dbuser' : 'DATABASE_USER',\n    'dbpass' : 'DATABASE_PASSWORD'\n}\n```\n\nBy default, the database name is `uptime` - if you want to use another name, change it. Update the `dbuser` and `dbpass` properties with the credentials that the database will use.\n\nIf you change the database name, make sure to edit schema.sql with `nano schema.sql` and update the name in the first two lines:\n\n```sql\nCREATE DATABASE `uptime`;\nUSE `uptime`;\n```\n\nThe Schema also includes a `ledStatus` table that stores the current status of the LEDs. The default pins listed above are used. If you are using other GPIO Pins, \nplease update the `INSERT` query on line 61 to the GPIO Pin Id that you are using:\n\n```sql\nINSERT INTO ledStatus (color, pin, status) VALUES ('red', 18, 0),('yellow',25,0),('green',12,0);\n```\n\nThe above table is used for turning off the Green LED during off hours using cron jobs explained below. \n\nNow you can run the schema.sql with the following command:\n```bash\nsource database/schema.sql\n```\n\n### Virtual Environment \u0026 Dependencies\n\nIn the main repo directory, install a virtual environment:\n```bash\napt install python3-venv\npython3 -m venv venv\n```\n\nActivate the virtual environment:\n```bash\n. venv/bin/activate\n```\n\nLet's install five more dependencies. Installing flask right away isn't necessary unless you want a UI for your web service:\n```bash\napt install python3-dev \napt install default-libmysqlclient-dev\napt install build-essential\npip install RPi.GPIO flask mysqlclient requests python-crontab\n```\n\nNote: If you are using Maria DB, chnage the second command above to the following:\n\n```bash\napt install libmariadbclient-dev\n```\n\n## Initialize Cron jobs\n\nThis app runs automatically via cron jobs. You can initialize some Cron Jobs that are put in place. Before proceeding, edit the `initCron.py` file and edit the two instances of the directory called `uptime` on \nline 17 to whatever you called your repository.\n\nAfter making the edit, run the script with the following command:\n\n```bash\npython3 initCron.py\n```\n\nThis script will add the cron jobs listed below to crontab as well as to the `cronSettings` MySQL table. Running `initCron.py` will clear your crontab and rewrite all defaults.\n\nCurrent Cronjobs:\n\n- **Check Sites** - by default, this will check sites every 15 minutes to see if they are online\n- **Enable/Disable Green LED** - by default, the Green LED will be turned off between 12:30 AM and 5:30 PM on weekdays and 1 am and 8 am on weekends. \nThis is the assumption that you will not be home to see the light. This will NOT affect the red and yellow lights. You can change the values in the init file or later in the crontab. \n- **Email Notification** - a cron will run 1 minute after the sites are checked to get a count of how many times a particular site has been down. If a site has been reported down 3 times in a row, \nit will trigger an email from the sender to recipient email address as specified in `config.py`.\n\nIf you want to add or update your cron after you've run `initCron.py`, you can easily edit the official crontab with the following command:\n\n```bash\ncrontab -e\n```\n\nYou can also update the database manually. You can also edit the `initCron.py` file and add a new method or edit an old one with your Cron settings and re-run the `initCron.py` script. \nI will add a way to programmatically update your settings in the future.  \n\n## Flask\n\nWeb service end points have been created with Flask that can be connected to via a frontend web application or a simple Post request. The end points are still being built. The information below will help you get \nthe flask app started as well as understand some of the basic endpoints. Most end points return JSON, some may return basic text. More details below. \n\n### Run Flask APP\n\n**Note:** _The flask app is still under construction. So far, a few web service end points have been created. More will be added._  \n\nRunning a flask app is fairly simple. To run apache in front of flask so that you can access your app without needing the port, read the [Apache](#apache) section below. \n\nThe controller for the Flask app is `upService.py`. Once you have flask installed in your virtual environment, you can start flask with the following commands:\n\n```bash\nexport FLASK_APP=upService.py\n```\n\nThen run the actual flask app:\n\n```bash\nflask run --host=0.0.0.0\n```\n\nYou can now access it from any computer on your network (assuming there are no firewall settings blocking this) by going to http://ip_addr:5000 - \nsubstitute `ip_addr` for the IP address for your pi. Hostname will also work in some instances depending on your network setup. \nThe apache section below will cover how to run your flask app through apache.\n\nDetails on the Flask app will be posted later. \n\n### Web Service Endpoints\n\nNote: All `GET` requests below have two URL parameters that they accept. They are `page` with a default value of 1 and `limit` with a default value of 25. \n\n#### 1. Sites\n\nYou can get a list of all sites being monitored with the `/getSites` endpoint. You can add a site with `/addSite` and pass in these items:\n\n- `siteName` - name of the site \n- `url` - url to be monitored\n- `email` - email address which will be contacted if the site goes down\n\nNew sites are automatically set to the active status, but can be turned off by changing the `active` property to `0`. \n\nYou can update a site by using the `/updateSite` endpoint. Along with the three properties above, send these additional two to update the site:\n\n- `id` - in order to edit a site, it must exist with an unique id in the database\n- `active` - change the active status to either 1 (active) or 0 (inactive)\n\nDelete functionality will be coming in a future release. It will be a soft delete (visibility will be set to 0 instead of removing the row from the database). \n\n#### 2. Update Status\n\nYou can run the website checks manually with `/updateStatus` - it may take a few seconds to a minute to return something depends on how many sites are being checked.\n\n#### 3. GET Current Status (Sites component replaces this)\n\nNote: This will eventually be deprecated since the Sites component handles everything here and more. \n\nYou can get the current status of all websites checked with this end point: `/getCurrentStatus` \n\nSites previously checked but removed from the list will return as `down`. \n\n#### 4. GET Activity\n\nEvery time the `uptime.py` script is run, it's recorded in the database. The `/getActivity` end point will grab you every entry. Note that this list can get very long.  \n\n#### 5. Other GET requests (will be documented later)\n\n1. `/getOutages`\n2. `/getDowntimeCounts`\n3. `/getCron/`\n4. `/getNotifications`\n\n#### POST and PUT requests\n\n1. `/overrideGreen` - override the database value that keeps the green light turned off. `PUT` request taking one parameter: `status` (0 or 1)\n2. `/checkFrequency` - Changes the frequency at which the regular site check runs. By default it's every 15 minutes. `PUT` request that takes three parameters: `cronName`, `cronVal`, and `enabled` (0 or 1)\n3. `/updateCron` - Changes the crontab values of placed cronjobs based on comment name. `PUT` request that takes 4 paramters: `comment` (unique identifier), `cronName`, `cronVal`, and `enabled` (0 or 1).\n4. '/updateSite' - new - will update sites\n\nMore will be added. Want me to add something specific, let me know!\n\n## Apache\n\n### Installation\n\nWe need to install Apache2 as well as Libapache WSGI module. To do so, run this command:\n\n```bash\nsudo apt install apache2 libapache2-mod-wsgi-py3 -y\n```\n\nThis will install the required depencencies. \n\nMove or copy the `activate_this.py` file from the `util/` dir into your venv folder.  You can do so with this command:\n\n```bash\ncp util/activate_this.py venv/bin/\n``` \n\nThe file will allow Apache to run your this application from from the virtual environment. The file was last copied on January 31, 2019. To look for updates or to get it directly from the source, you can run this command:\n\n```bash\ncd venv/bin\nwget https://raw.githubusercontent.com/pypa/virtualenv/master/virtualenv_embedded/activate_this.py\n```\n\nThis will enter your `venv/bin` dir and download the file from the source (source file does not change often). \n\nWe need to add a new configuration file to Apache. To do this run these:\n\n```bash\ncd /etc/apache2/sites-available\n```\n\nCreate a new .conf file:\n\n```bash\nsudo nano uptime.conf\n```\n\nEnter this new virtual host information in the file and save:\n```apacheconf\n\u003cVirtualHost *:80\u003e\n    ServerName uptimepi\n    WSGIDaemonProcess uptime user=pi group=www-data threads=5\n    WSGIScriptAlias /uptime /var/www/html/uptime/uptime.wsgi\n    \u003cDirectory /var/www/html/uptime\u003e\n        WSGIProcessGroup uptime\n        WSGIScriptReloading On\n        WSGIApplicationGroup %{GLOBAL}\n        Require all granted\n    \u003c/Directory\u003e\n\u003c/VirtualHost\u003e\n```\n\nThen we need to activate this new configuration file and disable the default one with these commands:\n\n```bash\nsudo a2ensite uptime.conf\nsudo a2dissite 000-default.conf\n```\n\nYou can then restart apache with `sudo service apache2 restart` now.\n\nAs the file suggests, we need the `uptime.wsgi` file in the  `/var/www/html/uptime/` directory so that our apache can access it. So let's do these commands:\n\n```bash\ncd /var/www/html\nsudo mkdir uptime\ncd uptime\n```\n\nA wsgi file is already provided in the `util/` directory of this repository. You can either create your own or copy the provided file with this command:\n\n```bash\nsudo cp /home/pi/uptime/util/uptime.wsgi /var/www/html/uptime/\n```\n\nAdjust the directory from `/uptime/` as you need to if you created your own directory. Note that you will need to update line 3 and 8 of `uptime.wsgi` with the correct directory as well. \n\nOnce this is done, restart apache if you haven't already, open a new browser and go to `http://ip_addr/uptime` - if all goes well, the app should load and you no longer \nneed to start Flask every time or use the port 5000. You can use any of the routes available to you. \n\nWhat are the benefits of running apache in front of your flask app?\n\nWhen we create our front-end site, we won't have to worry about CORS when requesting data. We can simply add our front-end to another folder in our web server and use this as one big app!\n\n## Backlog items:\n\nFor a better list of backlog items, check out the beta project on Github: https://github.com/naztronaut/RaspberryPi-Website-Uptime-Monitor/projects/1\n\n1. Web service access (currently in progress as a Flask app)\n2. Database integration - for reporting purposes (COMPLETE)\n3. Cron Jobs (COMPLETE)\n4. Ability to add and update cron jobs programmatically (partially complete)\n5. Notification via email (Mostly Complete) \n6. Front-End UI (in progress - Planning on creating a separate Angular app that consumes the Flask Web Service endpoints) \n \nWant to add an item to the backlog? Submit an issue. \n\n## Authors\n\n* **Nazmus Nasir** - [Easy Programming](https://www.easyprogramming.net)\n\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnaztronaut%2FeasyUptime","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnaztronaut%2FeasyUptime","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnaztronaut%2FeasyUptime/lists"}