Tag: monitor

  • Auto restart newrelic-daemon if crashed

    On a web server with Newrelic APM installed, graph stopped showing new data. On checking the newrelic log file, i get following error

    root@new-website-2:~# tail -f /var/log/newrelic/php_agent.log
    2021-02-08 17:21:27.453 +0000 (680439 680439) warning: daemon connect(fd=13 uds=@newrelic) returned -1 errno=ECONNREFUSED. Failed to connect to the newrelic-daemon. Please make sure that there is a properly configured newrelic-daemon running. For additional assistance, please see: https://newrelic.com/docs/php/newrelic-daemon-startup-modes
    

    This is because newrelic-daemon stopped working. To auto restart newrelic-daemon if it crash, do the following

    mkdir -p /usr/serverok
    vi /usr/serverok/newrelic
    

    Add following to the file

    #!/bin/bash
    # Author: ServerOK
    # Web: https://serverok.in/auto-restart-newrelic-daemon
    
    CURRENT_STATUS="$(/etc/init.d/newrelic-daemon status)"
    
    TIME_STAMP="$(date "+%Y-%m-%d %H:%M:%S")"
    
    if [[ ! "${CURRENT_STATUS}" =~ "newrelic-daemon is running" ]]
    then
        /etc/init.d/newrelic-daemon start
        echo -e "${TIME_STAMP} newrelic-daemon down\n"
    fi
    

    Make the script executable

    chmod 755 /usr/serverok/newrelic 
    

    Create a cronjob

    crontab -e
    

    Add following cronjob

    */5 * * * * /usr/serverok/newrelic > /var/log/newrelic/monitor.log 2>&1
    

    See Server Monitoring, Application Performance Monitor (APM)

  • PHP Script to monitor Apache/php-fpm

    I moved a web site to new dedicated server. But for some reason, php-fpm crashed. I increased the max_children settings, but it happend again. I do not want down time while i am investigating the problem. So i created a PHP script, that will check if site is working or not.

    Script have 2 part.

    health-check.php

    
    

    It is simple PHP script, that get a param and print it.

    This file is placed on root of your web site, so it can be accessed using URL http://yoursite/health-check.php

    monitor-server.php

    Create

    mkdir  /usr/serverok/
    vi  /usr/serverok/monitor-server.php
    

    Add following content

    
    

    On the script, replace YOUR_DOMAIN_HERE with your actual domain name.

    systemctl restart apache2 is for restart apache web server. If you use nginx, replace it. systemctl restart php7.2-fpm restart php-fpm, if you have differnt version of php, you need to change it.

    The script is generate a random number, pass it to health-check.php script. Compared the value returned with generated random number to make sure the value is correct. If web server or php-fpm fail, this check will fail.

    Now set a cronjob

    crontab -e
    

    Add

    */5 * * * * /usr/bin/php /usr/serverok/monitor-server.php
    

    Related Posts

    Server Monitoring