DEV Community

Cover image for I Wrote a simple Up-time Monitor in Golang for My PHP Server
Toby Chui
Toby Chui

Posted on

I Wrote a simple Up-time Monitor in Golang for My PHP Server

Up-time monitors are crucial because they help you keep track of your servers' availability and can help you detect outages quickly. This can help prevent prolonged downtime and ensure that your users are able to access your services as much as possible.

If you want to get started with an up-time monitor, there are plenty of options available like Pingdom, Datadog, and NewRelic. You can also choose to write one yourself if you know how to code.

As I only need a really simple and effective way to oversee my servers, and I already got a PHP server running for my blog, I decided to make one with PHP.

One problem: How to make PHP ping a website every x minutes?

As my server was built from an old Windows 7 PC, I decided to write a small Go program to make the website status check and store the data in RAM. The data was never written to disk unless it is required because writing data constantly to an SSD will quickly use up all its R/W count.

Then you might be wondering, how could I pass information to PHP for displaying then? To access the results by the PHP script, I setup a simple http listener on the Go program and allow php to request the buffered online status from the go program using HTTP request.

ui

This up-time monitor is simple to use and easy to integrate to your existing LAMP infrastructure. Simply download the binary and the php script, move them to a suitable location and start the binary. Your php will then show the uptime information of your servers every time you request that script.

I am only using this for my web servers. However, it is designed to be extendable in which different protocols can also be implemented without the need to change anything on the front-end side. If you want to try it out, here is the Github link for the project. Feel free to PR this project if you want more protocols to be supported!

GitHub logo tobychui / imusutm

Service up-time monitor for PHP server but written in Golang

imusutm

Service up-time monitor for PHP server but written in Golang

Usage

This project contain two parts: The PHP section and the Go section. The PHP section contains the UI and the required script to show the UI, and the Go section in charge of pinging the listed URLs at a regular interval to keep track of their online states.

Setup Backend Monitoring Server

Create a file named "config.json" and place it in the same folder to the binary executable.

{
 "Targets": [
    {
        "ID": "imus_homepage", //ID of the target
        "Name": "imuslab Homepage", //Name to show on the UI
        "URL": "https://imuslab.com", //URL to request
        "Protocol": "https" //Protocol to check online
    }
    //More endpoint here
 ]
 "Interval": 300, //Update interval in seconds
 "LogToFile": false, //Log results to file
 "RecordsInJson": 288
Enter fullscreen mode Exit fullscreen mode

Top comments (0)