1

I just deployed my webserver using Apache in CentOS and I was wondering if anybody has any good ideas in how to check every specified amount of time if the server goes down and then I can use postfix to email me when this happens so I can go back to my server right away and fix the problem as well to see what caused the problem. I'm guessing that many websites use some software/script to let them know when their service goes down before clients start complaining about the problem.

  • 1
    You need to consider the case in which the webserver loses connectivity to the Internet. The server may not be down but it will be unreachable. To improve your check, the script would need to run on a remote machine and then curl something small ( favicon maybe ) from your site. You could do this at intervals and then set up the remote server to email you. – rcjohnson Jun 3 '15 at 21:05
1

You could have a script in your server that makes all kind of test among them if the server is alive, here's one:

#!/bin/bash
    date;
    echo "uptime:"
    uptime
    echo "Currently connected:"
    w
    echo "--------------------"
    echo "Last logins:"
    last -a |head -3
    echo "--------------------"
    echo "Disk and memory usage:"
    df -h | xargs | awk '{print "Free/total disk: " $11 " / " $9}'
    free -m | xargs | awk '{print "Free/total memory: " $17 " / " $8 " MB"}'
    echo "--------------------"
    start_log=`head -1 /var/log/messages |cut -c 1-12`
    oom=`grep -ci kill /var/log/messages`
    echo -n "OOM errors since $start_log :" $oom
    echo ""
    echo "--------------------"
    echo "Utilization and most expensive processes:"
    top -b |head -3
    echo
    top -b |head -10 |tail -4
    echo "--------------------"
    echo "Open TCP ports:"
    nmap -p- -T4 127.0.0.1
    echo "--------------------"
    echo "Current connections:"
    ss -s
    echo "--------------------"
    echo "processes:"
    ps auxf --width=200
    echo "--------------------"
    echo "vmstat:"
    vmstat 1 5

This would give you the following results:

./Server-Health.sh

Tue Jul 16 22:01:06 IST 2013
uptime:
 22:01:06 up 174 days,  4:42,  1 user,  load average: 0.36, 0.25, 0.18
Currently connected:
 22:01:06 up 174 days,  4:42,  1 user,  load average: 0.36, 0.25, 0.18
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
tecmint   pts/0    116.72.134.162   21:48    0.00s  0.03s  0.03s sshd: tecmint [priv]
--------------------
Last logins:
tecmint   pts/0        Tue Jul 16 21:48   still logged in    116.72.134.162
tecmint   pts/0        Tue Jul 16 21:24 - 21:43  (00:19)     116.72.134.162
--------------------
Disk and memory usage:
Free/total disk: 292G / 457G
Free/total memory: 3510 / 3838 MB
--------------------
OOM errors since Jul 14 03:37 : 0
--------------------
Utilization and most expensive processes:
top - 22:01:07 up 174 days,  4:42,  1 user,  load average: 0.36, 0.25, 0.18
Tasks: 149 total,   1 running, 148 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.1%us,  0.0%sy,  0.0%ni, 99.3%id,  0.6%wa,  0.0%hi,  0.0%si,  0.0%st

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
    1 root      20   0  3788 1128  932 S  0.0  0.0   0:32.94 init
    2 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthreadd
    3 root      RT   0     0    0    0 S  0.0  0.0   0:14.07 migration/0
  • just to belabor rcjohnson's point - if your server itself goes down, any monitoring that runs on that server also goes down, so you wouldn't get alerted. – Jeff Schaller Aug 8 '16 at 11:21
5

there are tools like nagios or icinga that can monitor a server.

they can monitor a lot more that just http url: things linke ssh, mysql, if disk space gets scarce - -and many more. may be a bit overkill for your use? the services write mails if things fail.

  • Just out of curiosity do you know any simple script that can do this task without installing software? – VaTo Jun 3 '15 at 20:02
  • you could write a python script using requests ( docs.python-requests.org/en/latest ) or simply a bash script using curl. – hiro protagonist Jun 3 '15 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.