RSS
 

Checking Up On Your Linux Server from a Webpage

21 Aug

I was playing with CGI. I wrote a simple cgi web app that allows you to see what is going on with your Linux server from a web page. This is just a simple app for educational purposes. Its not Nagios by any means:)

Linux Commands
Fun linux commands used by the app to get server information.

hostname
This will display the hostname of your system on the terminal

uptime
Displays how long the system has been running and the number of users currently logged in.

w
Lists all the users logged into the system, what programs they are running, when they logged in and how active or idle they have been.

free -m
Displays the amount of free memory on the system.

df -h
Displays information about the hard drive, displays the size, used and available space.

ps aux
ps command displays the processes running on the system. The fun thing about the ps command is that you can format the results and choose which parameters to include in the results. You can also sort the results using –sort. An interesting variation of the ps command is

ps -eo start,pid,user,args --sort user
This will display all the processes running on the system, arranged by Time STARTED, PID , USER of that process and COMMAND they are running/location of the executable all ordered by Process USER. More details about the ps command here.

pstree
Displays a tree diagram of all processes running on the system. It shows both the parent and their child (spawn) processes, so it gives you a good idea of what is going on in the system.

System Files
Some system files contain important information about the server. The information from these files are displayed on the terminal using the cat command.

cat /etc/issue
Displays the System’s Linux Distro and the version number.

cat /proc/version
Displays the systems kernel version and build information.

cat /proc/cpuinfo
Displays the system’s CPU information

Displaying Results of a Shell Script on a Webpage
We do this using CGI. CGI is an interface for executing scripts, receiving and processing requests from clients on a web server. Most popular web servers like Apache and lighthttpd support CGI. CGI scripts can be written in any language. To get the webserver to execute a cgi script simply put that script in the cgi-bin directory.
Debian/Ubuntu has its cgi-bin directory at /usr/lib/cgi-bin and CentOS/Fedora/RedHat has its cgi-bin directory at /var/www/cgi-bin. You must have Apache,lighthttpd or a web server that supports CGI running on the system.

Nixcraft already wrote an excellent tutorial on displaying results from shell scripts on a webpage, read it first to see how to do it. Click here for the tutorial. The app I wrote extends the cgi script in that tutorial. I added menu options and more features to it.

SysInfo: The Server Check Up CGI Web App
The APP consists of 4 CGI scripts.

index.cgi – Displays the basic information about about the system.

cpu-info.cgi -Displays the systems CPU details (vendor,model,speed,core etc).

ps.cgi -Displays the Processes running on the system by TIME STARTED, PROCESS ID, USER,COMMAND.

pstree.cgi -Displays the running processes in a tree diagram showing the parent and their child processes.

All the scripts assume they are running from the directory cgi-bin/sysinfo/. To test the App manually, create a folder in your cgi-bin directory called sysinfo.

For Ubuntu/Debian use:
mkdir /usr/lib/cgi-bin/sysinfo

For CentOS/Fedora:
mkdir /var/www/cgi-bin/sysinfo

Now create the scripts with your favorite text editor and save them in the cgi-bin/sysinfo/ folder. You can copy and paste the source code of all the scripts from here:http://whisperservers.com/sysinfo/. you only need to create index.cgi,cpu-info.cgi,ps.cgi and pstree.cgi.

Next step is to make all the scripts executable:
sudo chmod +x index.cgi cpu-info.cgi ps.cgi pstree.cgi

Now you are done, to view the App, open your web browser and type on the address bar http://your-ip/cgi-bin/sysinfo/index.cgi or http://localhost/cgi-bin/sysinfo/index.cgi

To make the app easy to install and test, I wrote a script that will install it for you, so you dont have to do any of the steps described above, you simply run the installer script and open your web browser to use the app.

Using the installer script to Install the App;
Step 1. Download the installer script with

wget http://whisperservers.com/sysinfo/install-sysinfo

Step 2. Make the installer script executable

chmod +x install-sysinfo

step 3. Run the installer

./install-sysinfo

Step 4. Now test the app, open your web browser and type on the address bar http://your-ip/cgi-bin/sysinfo/index.cgi or http://localhost/cgi-bin/sysinfo/index.cgi

The source code for the installer script, install-sysinfo;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash
#:Title   :sysinfo - Installer
#:Date    :8/15/2010
#:Author  :Obi Akubue
#:Version :0.1
#description: Installs the Sysinfo cgi scripts
 
red='\e[0;31m'
RED='\e[1;31m'
blue='\e[0;34m'
BLUE='\e[1;34m' #light blue
cyan='\e[0;36m'
CYAN='\e[1;36m'
GREEN='\e[1;32m'
YELLOW='\e[1;33m'
WHITE='\e[1;37m'
NC='\e[0m' # No Color
 
#begining  messages
 if [ "$1" = "-u" ]; then
    #option to install on Ubuntu/Debian
        echo  -e "${GREEN} Welcome to Sysinfo Installer"
        echo " Installing on Ubuntu/Debian Linux ..."
        echo -e "${WHITE} Press ENTER to continue or Ctrl-C to Quit"
 
        echo -e "${NC}"
 
    elif [ "$1" = "-c" ]; then
        #option to install on CentOS/Fedora/Redhat
        echo  -e "${GREEN} Welcome to Sysinfo Installer"
        echo " Installing on CentOS/Fedora/Redhat Linux ..."
        echo -e "${WHITE} Press ENTER to continue or Ctrl-C to Quit"
        echo -e "${NC}"
    else
        echo  -e "${GREEN} Welcome to Sysinfo Installer"
        echo -e "${WHITE} "
        echo "To install on Ubuntu/Debian Abort and type ./sysinfo-installer -u"
        echo "To install on CentOS/Fedora/Redhat Abort and type ./sysinfo-installer -c"
        echo "Press Ctrl-C to Abort"
        echo -e "${NC}"
        read key
        exit
 
   fi
 
 read key
 
#
if [ "$1" = "-u" ]; then
#is Ubuntu/Debian OS
sudo mkdir /usr/lib/cgi-bin/sysinfo
cd /usr/lib/cgi-bin/sysinfo
 
elif [ "$1" = "-c" ]; then
#is CentOS/Fedora/Redhat
sudo mkdir /var/www/cgi-bin/sysinfo
cd /var/www/cgi-bin/sysinfo
fi
 
sudo wget http://whisperservers.com/sysinfo/index
sudo wget http://whisperservers.com/sysinfo/ps
sudo wget http://whisperservers.com/sysinfo/pstree
sudo wget http://whisperservers.com/sysinfo/cpu-info
 
sudo mv index index.cgi
sudo mv ps ps.cgi
sudo mv pstree pstree.cgi
sudo mv cpu-info cpu-info.cgi
 
sudo chmod +x index.cgi cpu-info.cgi ps.cgi pstree.cgi
 
echo  -e "${GREEN} SysInfo has been successfully Installed"
echo "To test it on your browser open"
echo -e "${YELLOW} http://your-ip/cgi-bin/sysinfo/index.cgi or http://your-hostname/cgi-bin/sysinfo/index.cgi"
 echo -e "${NC}"

Source Code for the App: SysInfo
Source for index.cgi.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/bin/bash
#:Title   :sysinfo
#:Date    :8/15/2010
#:Author  :Obi Akubue
#:Version :0.1
#description: Displays General System Information on a webpage
echo "Content-type: text/html"
echo ""
echo "<html><head><title>SysInfo"
echo "</title></head><body>"
 
echo "<h1>General system information</h1>"
echo ""
 
echo "Home |"
echo "<a href="/cgi-bin/sysinfo/cpu-info.cgi">CPU Info</a> |"
echo "<a href="/cgi-bin/sysinfo/ps.cgi">Running Pocesses</a> |"
echo "<a href="/cgi-bin/sysinfo/pstree.cgi">Process Tree</a> |"
echo "<a href="">Refresh</a> "
echo ""
echo ""
echo ""
 
echo "<p><b>Hostname: </b>$(hostname -s)</p>"
echo "<p><b>OS: </b>$(cat /etc/issue)</p>"
echo "<p><b>Uptime: </b>$(uptime)</p>"
 
echo "<h1>Logged in users</h1>"
echo "<pre> $(w) </pre>"
 
echo "<h1>Memory Info</h1>"
echo "<pre> $(free -m) </pre>"
 
echo "<h1>Disk Info:</h1>"
echo "<pre> $(df -h) </pre>"
 
echo "<center><b>Running: </b> $(cat /proc/version)</center><p></p>"
echo "<center>Information generated on $(date)</center>"
echo "</body></html>"

Source for cpu-info.cgi

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/bin/bash
#:Title   :sysinfo ->cpu info
#:Date    :8/15/2010
#:Author  :Obi Akubue
#:Version :0.1
#description: Displays General System Information on a webpage
echo "Content-type: text/html"
echo ""
echo "<html><head><title>CPU Info"
echo "</title></head><body>"
 
echo "<h1>General system information</h1>"
echo ""
 
echo "<a href="/cgi-bin/sysinfo/index.cgi">Home</a> |"
echo "CPU Info |"
echo "<a href="/cgi-bin/sysinfo/ps.cgi">Running Pocesses</a> |"
echo "<a href="/cgi-bin/sysinfo/pstree.cgi">Process Tree</a> |"
echo "<a href="">Refresh</a> "
echo ""
echo ""
echo ""
echo "<p><b>Hostname: </b>$(hostname -s)</p>"
echo "<p><b>OS: </b>$(cat /etc/issue)</p>"
echo "<p><b>Uptime: </b>$(uptime)</p>"
 
echo "<h1>CPU Info</h1>"
echo "<pre> $(cat /proc/cpuinfo) </pre>"
 
echo "<center><b>Running: </b> $(cat /proc/version)</center><p></p>"
echo "<center>Information generated on $(date)</center>"
echo "</body></html>"

Source for ps.cgi

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/bin/bash
#:Title   :sysinfo ->Running Processes
#:Date    :8/15/2010
#:Author  :Obi Akubue
#:Version :0.1
#description: Displays General System Information on a webpage, works on linux/unix/bsd
 
echo "Content-type: text/html"
echo ""
echo "<html><head><title>CPU Info"
echo "</title></head><body>"
 
echo "<h1>General system information</h1>"
echo ""
 
echo "<a href="/cgi-bin/sysinfo/index.cgi">Home</a> |"
echo "<a href="/cgi-bin/sysinfo/cpu-info.cgi">CPU Info</a> |"
echo "Running Pocesses |"
echo "<a href="/cgi-bin/sysinfo/pstree.cgi">Process Tree</a> |"
echo "<a href="">Refresh</a> "
echo ""
echo ""
echo ""
echo "<p><b>Hostname: </b>$(hostname -s)</p>"
echo "<p><b>OS: </b>$(cat /etc/issue)</p>"
echo "<p><b>Uptime: </b>$(uptime)</p>"
 
echo "<h1>Running Processes</h1>"
echo "<pre> $( ps -eo start,pid,user,args --sort user) </pre>"
 
echo "<center><b>Running: </b> $(cat /proc/version)</center><p></p>"
echo "<center>Information generated on $(date)</center>"
echo "</body></html>"

Source for pstree.cgi

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/bin/bash
#:Title   :sysinfo ->Process Tree
#:Date    :8/15/2010
#:Author  :Obi Akubue
#:Version :0.1
#description: Displays General System Information on a webpage, works on linux/unix/bsd
 
echo "Content-type: text/html"
echo ""
echo "<html><head><title>CPU Info"
echo "</title></head><body>"
 
echo "<h1>General system information</h1>"
echo ""
 
echo "<a href="/cgi-bin/sysinfo/index.cgi">Home</a> |"
echo "<a href="/cgi-bin/sysinfo/cpu-info.cgi">CPU Info</a> |"
echo "<a href="/cgi-bin/sysinfo/ps.cgi">Running Pocesses</a> |"
echo "Process Tree |"
echo "<a href="">Refresh</a> "
echo ""
echo ""
echo ""
echo "<p><b>Hostname: </b>$(hostname -s)</p>"
echo "<p><b>OS: </b>$(cat /etc/issue)</p>"
echo "<p><b>Uptime: </b>$(uptime)</p>"
 
echo "<h1>Running Processes</h1>"
echo "<pre> $(pstree) </pre>"
 
echo "<center><b>Running: </b> $(cat /proc/version)</center><p></p>"
echo "<center>Information generated on $(date)</center>"
echo "</body></html>"
 
1 Comment

Posted in Linux

 

Tags: , , , , ,

 
Premium Wordpress Plugin