January 19th, 2023

Send a Daily Report of WHM Server Stats to Slack

In this practical example, I will outline the process for sending server stats from WHM managed machines to a central Pimcore installation. Once the data is in Pimcore, we can then digest the information into a report and send it to a Slack channel to help with pro-active observability.

Creating the StatsUser Class

Create a new class in Pimcore called StatsUser. Once the class is created, add a layout component, and then eight Text / Input fields. The fields are named AuthToken, DiskTotal, DiskFree, PramTotal, PramFree, VramTotal, VramFree, and Uptime. Save changes to the class, and close the editor tab.

Keying a StatsUser Account Object

Create a folder to keep the objects in. Use the name of the class as the name of the folder, in this case the folder is named /StatsUser/. In this folder, create an instance of the StatsUser class and set the object's key to the intended Basic Authentication username, which will also serve as the name the stats are associated with when they appear in Slack.

Creating the Bash Script

The bash script will be installed in the /root/stats.sh file for the WHM server that will be reporting its stats back to Pimcore.

#!/bin/bash

# CONFIG
CHOST='https://my-domain.com/stats'
CUSER='stats_username'
CPASS='stats_password'

# DISK METRICS
DISKT=`df / | awk '(NR>1){print $2}'`
DISKF=`df / | awk '(NR>1){print $4}'`

# PHYSICAL RAM METRICS
PRAMT=`grep MemTotal /proc/meminfo | awk '{print $2}'`
PRAMF=`grep MemAvailable /proc/meminfo | awk '{print $2}'`

# VIRTUAL RAM METRICS
VRAMT=`grep SwapTotal /proc/meminfo | awk '{print $2}'`
VRAMF=`grep SwapFree /proc/meminfo | awk '{print $2}'`

# UPTIME SINCE TIMESTAMP
UTIME=`uptime -s`

# CSV OUTPUT
OUTPUT="${DISKT},${DISKF},${PRAMT},${PRAMF},${VRAMT},${VRAMF},${UTIME}"

# CURL PARAMETERS
HUPB64=`echo -n "${CUSER}:${CPASS}" | base64`
HBAUTH=`echo -n "Authorization: Basic ${HUPB64}"`
HCTYPE=`echo -n "Content-Type: text/plain"`

# SEND IT!
curl --location --request POST "${CHOST}" --header "${HBAUTH}" --header "${HCTYPE}" --data "${OUTPUT}"

Before executing the script, the CHOST, CUSER, and CPASS variables should be set When the script is run, the resulting curl content being sent will look similar to:

823621448,498264144,60380836,51128936,2047996,619152,2021-07-01 17:24:29

 

 

 

 

The third step is to create the controller and the route where POST requests can be made.

/src/AppBundle/Controller/StatsController.php

namespace AppBundle\Controller;

use Pimcore\Controller\FrontendController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class StatsController extends FrontendController {

    /**
     * @Route("/stats", name="stats")
     */
    public function stats( Request $request ) {

        return $this->json( [], 200 );

    }

}

Topics

## PAGE REGION UNDER MAINTENANCE ##

Related Articles

## PAGE REGION UNDER MAINTENANCE ##