📜 ⬆️ ⬇️

Monitoring system on BASH

Monitoring system: why?


Many of you who were responsible for a small subnet, faced with the problem of taking into account the performance of a couple of dozen machines. Or you just wanted to be able at any time from anywhere in the world to find out how your torrent swing left at home is feeling.

Personally, I developed this system to solve the problem of hidden remote monitoring of the computers entrusted to me. At the implementation stage, I was offered to get some profit from this project and do everything on BASH as a project for some conference.

... and now, digging trash, I found the source. While working as a web programmer was not in vain, it was decided to completely override and expand the functionality. Actually, let's start ...
')

Platform Description, Dependencies


Not so long ago, I upgraded to the 12.10th version of my beloved Kubuntu (Ubuntu with KDE as WM, GNOME can not stand it). The system is pristine clean, no manipulations have been made with it, so there will be no packages in it that you don’t have.
Kernel 3.5.0-21, KDE. For the system itself, we need additional packages that can be found in the standard repository. We type the following:
sudo -s apt-get install perl apt-get install libnotify-bin apt-get install lm-sensors apt-get install sysstat apt-get install apache2 exit 

We will need the pearl to execute the line processor script, libnotify to display a notification, lm-sensors to take the temperature, sysstat to collect statistics on I / O, apache2 the web server to display. Well, by itself, any text editor with syntax highlighting is needed.

Implementation


At first I made a pattern on the divas, in some cases the beginning of the teaser for the film “Start”, but in general the structure of the DOM tree is correct. Next, we design a system that will fill this template. To do this, we write a script on BASH. To do this, you should know that for our (Ubuntu) distribution, the default directory for the webserver is Apache / var / www. Therefore, if nothing except our system is in the public domain, the output will be implemented immediately in index.html.

The script itself is extremely simple: the echo command outputs the input parameter to stdout, we redirect this exhaust to a file. So we can transfer in html from BASH template. From the point of view of the theory of algorithms, we get the already specialized code.
 #!/bin/bash export DISPLAY=:0.0; echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <HTML> <!-- Coded and designed by 19N4T0V --> <head> <meta http-equiv="Content-Type" content="text/html; charset=KOI8-R"> <script type="text/javascript" src="jquery.js"></script> <title>Kansatsu</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <div class="leftline"> <a href=""><div class="header"></div></a> <ul class="leftmenu"> <a href="#summary"><li>SUMMARY</li></a> <a href="#IO"><li>I/O STATS</li></a> <a href="#temperature"><li>TEMPERATURE</li></a> <a href="#HDD"><li>HDD</li></a> <a href="#RAM"><li>RAM</li></a> <a href="#net"><li>NETWORK</li></a> <a href="#PROC"><li>PROC</li></a> </ul> </div> <div class="content"> <!--ABOUT BEGINS HERE--> <div class="block"> <div class="bl_head"> ABOUT </div> <div class="bl_info"> KANSATSU monitoring system is an opensource project. License is <a href="http://www.gnu.org/copyleft/"><font color="red">GNU copyleft</font></a>. This is the simple scripts written in BASH and Perl, running as a cron job . Everything you can see there is just responding to console comands. Author - <a href="http://19N4T0V.PNZ.RU/"><font color="red">19N4T0V</font></a>. If you want to receive the code to use or improve it - write me. <br> Thanks to <a href="http://linux.org.ru/"><font color="red">LOR</font></a> users for help and advices.<br> DEPENDS: lm-sensors, libnotify-bin, perl, sysstat. </div> </div> <!--SUMMARY BEGINS HERE--> <div class="block" id = "summary"> <div class="bl_head"> SUMMARY </div> <div class="bl_info"> <div class="line"> <div class="tit"> HOSTNAME </div> <div class="tty">' > /var/www/index.html hostname >> /var/www/index.html; echo ' </div> </div> <div class="line"> <div class="tit"> UPTIME </div> <div class="tty">' >> /var/www/index.html; uptime | sed 's/,.*//' | sed 's/.*up //' >> /var/www/index.html; echo ' </div> </div> <div class="line"> <div class="tit"> TIMESTAMP </div> <div class="tty">' >>/var/www/index.html; date >> /var/www/index.html; echo ' </div> </div> <div class="line"> <div class="tit"> KERNEL </div> <div class="tty">'>> /var/www/index.html; uname -s >> /var/www/index.html; echo ' </div> </div> <div class="line"> <div class="tit"> VERSION </div> <div class="tty">' >> /var/www/index.html; uname -r >> /var/www/index.html; echo ' </div> </div> <div class="line"> <div class="tit"> HARDWARE </div> <div class="tty">' >> /var/www/index.html; uname -m >> /var/www/index.html; echo ' </div> </div> <div class="line"> <div class="tit"> PROCESSOR </div> <div class="tty">' >> /var/www/index.html; uname -p >> /var/www/index.html; echo ' </div> </div> <div class="line"> <div class="tit"> PLATFORM </div> <div class="tty">' >> /var/www/index.html; uname -i >> /var/www/index.html; echo ' </div> </div> <div class="line"> <div class="tit"> OS </div> <div class="tty">' >> /var/www/index.html; uname -o >> /var/www/index.html; echo ' </div> </div> </div> </div> <!--IO BEGINS HERE--> ' >> /var/www/index.html; echo ' <div class="block" id ="IO"> <div class="bl_head"> I/O STATS </div> <div class="bl_info"> ' >> /var/www/index.html; iostat -h -x > /var/www/tmp; perl /var/www/scr.pl; echo ' </div> </div> </div> </div> <!--TEMPERATURE BEGINS HERE--> <div class="block" id ="temperature"> <div class="bl_head"> TEMPERATURE </div> <div class="bl_info"> <div class="line"> <div class="tit"> CHIPSET CURRENT </div> <div class="tty">' >> /var/www/index.html; sensors -u | grep "temp1_input" | sed 's/temp1_input://' | sed 's/ *//g' | sed 's/\.000//' >> /var/www/index.html; echo ' </div> </div> <div class="line"> <div class="tit"> CHIPSET CRITICAL </div> <div class="tty">' >>/var/www/index.html; sensors -u | grep "temp1_crit" | sed 's/temp1_crit://' | sed 's/ *//g' | sed 's/\.000//' >> /var/www/index.html; echo ' </div> </div> <div class="line"> <div class="tit"> CORE1 CURRENT </div> <div class="tty">' >> /var/www/index.html; sensors -u | grep "temp2_input" | sed 's/.*: //' | sed 's/.000//' >> /var/www/index.html; echo ' </div> </div> <div class="line"> <div class="tit"> CORE1 CRITICAL </div> <div class="tty">' >> /var/www/index.html; sensors -u | grep "temp2_crit:" | sed 's/.*: //' | sed 's/.000//' >> /var/www/index.html; echo ' </div> </div> <div class="line"> <div class="tit"> CORE2 CURRENT </div> <div class="tty">' >> /var/www/index.html; sensors -u | grep "temp3_input" | sed 's/.*: //' | sed 's/.000//' >> /var/www/index.html; echo ' </div> </div> <div class="line"> <div class="tit"> CORE2 CRITICAL </div> <div class="tty">' >> /var/www/index.html; sensors -u | grep "temp3_crit:" | sed 's/.*: //' | sed 's/.000//' >> /var/www/index.html; echo ' </div> </div> </div> </div> <!--HDD BEGINS HERE--> <div class="block" id="HDD"> <div class="bl_head"> HDD </div> <div class="bl_info">' >> /var/www/index.html; df -a -h > /var/www/tmp; perl /var/www/scr.pl; echo ' </div> </div> <!--RAM BEGINS HERE--> <div class="block" id="RAM"> <div class="bl_head"> RAM </div> <div class="bl_info">' >> /var/www/index.html; free -l -h > /var/www/tmp; perl /var/www/scr.pl; echo '</div></div> <!--NETWORK BEGINS HERE--> <div class="block" id="net"> <div class="bl_head"> NETWORK </div> <div class="bl_info">' >> /var/www/index.html; ifconfig > /var/www/tmp; perl /var/www/scr.pl; echo ' </div> </div> <!--PROC BEGINS HERE--> <div class="block" id="PROC"> <div class="bl_head"> PROC </div> <div class="bl_info">' >> /var/www/index.html; top -n1 -b > /var/www/tmp; perl /var/www/scr.pl; echo ' </div> </div>' >> /var/www/index.html; #footer, closing "content" div and finishing DOM tree echo '</div> </body> </html>' >> /var/www/index.html; notify-send "KANSATSU" "Report has been updated"; 


We see redirection of the echo output stream in abundance. So it turns out the pattern. To fill the template, use the same output stream redirection system for regular commands. If we look in more detail, we will see the following:
 hostname >> /var/www/index.html; uptime | sed 's/,.*//' | sed 's/.*up //' >> /var/www/index.html; date >> /var/www/index.html; uname -s >> /var/www/index.html; uname -r >> /var/www/index.html; uname -m >> /var/www/index.html; uname -p >> /var/www/index.html; uname -i >> /var/www/index.html; uname -o >> /var/www/index.html; iostat -h -x > /var/www/tmp; perl /var/www/scr.pl; sensors -u | grep "temp1_input" | sed 's/temp1_input://' | sed 's/ *//g' | sed 's/\.000//' >> /var/www/index.html; sensors -u | grep "temp1_crit" | sed 's/temp1_crit://' | sed 's/ *//g' | sed 's/\.000//' >> /var/www/index.html; sensors -u | grep "temp2_input" | sed 's/.*: //' | sed 's/.000//' >> /var/www/index.html; sensors -u | grep "temp2_crit:" | sed 's/.*: //' | sed 's/.000//' >> /var/www/index.html; sensors -u | grep "temp3_input" | sed 's/.*: //' | sed 's/.000//' >> /var/www/index.html; sensors -u | grep "temp3_crit:" | sed 's/.*: //' | sed 's/.000//' >> /var/www/index.html; df -a -h > /var/www/tmp; perl /var/www/scr.pl; free -l -h > /var/www/tmp; perl /var/www/scr.pl; ifconfig > /var/www/tmp; perl /var/www/scr.pl; top -n1 -b > /var/www/tmp; perl /var/www/scr.pl; notify-send "KANSATSU" "Report has been updated"; 

This is the skeleton of our system. We can say that in this form it is a console version for liks2, for example. Consider the commands and give comments:
hostname returns the name of the host for which information is being collected.
uptime returns the system uptime. In our case, it goes along the assembly line to sed, a string editor, where we extract only digits from the exhaust command of the replacement command: hours and minutes.
uname with various parameters - general information about the machine: architecture of iron, processor, core, OS ...
The iostat with the -x option gives an extended description of the I / O operation. The key -h promises to make the output readable.
The sensors return the temperature of my two cores and the temperature of the mother. The -u switch gives an extended output, then the exhaust goes along the conveyor to grep, which allows you to select one line from the entire output with the substring entered, then in sed, where we again remove the extra characters with regexp. I have only three sensors, therefore I display 6 lines: current and critical temperatures.
df will tell us about the hard disk, with the -a flag - about all partitions, with the -h flag will allow the person to disassemble the exhaust.
free - a report on memory usage. Here with flags for easy output and full report.
ifconfig - full report on all network interfaces. I took it as is.
top displays in the infinite loop the most voracious processes. I used the -b flag to output to the file, which removes special characters and with the -n1 flag — I specify the number of readings. It should be noted that with such a set of flags I received a list of all processes. If we consider that sometimes you need to see who is doing what on a remote machine, it is easily tracked by this list.
And finally, notify-send gives a system message about a successful report update.

Perl script


And no one kept silent about anything! Just for this you need a separate topic. I installed everything on divas and did not want to use a single table. In cases when I received a single value from the utility, I packed its description and the value itself into two diva in one line. All the columns, everything is beautiful. But now I get whole tables at the output. I could put them in html as is, using the pre tag, thereby retaining the original formatting. But in this case, the long tables will be unreadable, because for the hover div event of the line, which is each row, I have the highlight of the entire row set so that I don’t have to apply a ruler to the monitor or move the cursor along the row to the desired column. It is entirely personal that this trend should continue. For this, in fact, I made the following knight's move: if you take a closer look, wherever perl is used, the line above, the exhaust is not directed to html, but to / var / www / tmp. This is a text file. The pearl script should read this file line by line and “dress” each line in a div, then spit it out in html. Well, it is said - done:
 #! perl -w open (STREAM_IN, '/var/www/tmp');# || die "Can't open STREAM_IN\n"; open (STREAM_OUT, '>> /var/www/index.html');# || die "Can't open STREAM_OUT\n"; $s1 = '<div class="line">'; $s2 = '</div>'; while ($curr = <STREAM_IN>) { chomp($curr); $curr=~s/\s/\ /g; $out = $s1.$curr.$s2; print (STREAM_OUT $out); }; close STREAM_IN; close STREAM_OUT; 


From the code it is clear that we take a line, cut off the newline character, change spaces to codes (the table is formed by the console with spaces, if you give them in html as is - there will be porridge). Statically set the auxiliary lines and concatenated we get one line that is appended to the end of the html report. From the script code on BASH it is clear that echo follows after similar constructions, which hurries to close the divas.

Such a script works with any contents of the same file and records it to the end of another file. I always send the output of the table to a temporary file on the PER record, it clears itself, then the script is called, which this output leads to a common template and DRAWS to html. One script for all times).

Additional files, final touches


It is clear that you need to write a style sheet. I pursued the principle of KISS, I wanted maximum functionality and minimum workload. I chose the following solution: on the left there will be fixed links to each report block, and the report itself will be scrolled on the right.

 html { height:100%; width:100% } body { height:100%; width:100%; padding:0px; margin:0; color:#000; font-family:Arial; font-size:14px; background-color:#FFF; } .leftline { width:210px; overflow:hidden; float:left; height:100%; position:fixed; top:0px; left:0px; } .header { width:100%; float:none; background:url(logo.png) top left no-repeat; height:160px; } .leftmenu { padding:0px; } .leftmenu li { width:200px; height:30px; background:#000; font-size:24px; line-height:1.3; float:none; margin:10px 10px 10px 0px ; text-align:right; padding-right:10px; list-style:none } .leftmenu li:hover { background:#FF0000; } a { color:#FFF; text-decoration:none; } .content { } .block { float:none; margin-top:20px; margin-left:250px; margin-right:50px; border-left:2px black solid; } .bl_head { color:#fff; background:#000; padding:10px 0px 10px 10px; } .bl_info { color:#000; padding-left:10px; padding-top:5px; } .line { display:block; height:20px; cursor:default; } .line:hover { background:red; } .tit { width:200px; float:left; } .tty { } 


By code - that's it. You need only 2 more files and set permissions. The first file is the logo itself. Half an hour GIMP'a and voila! The second file is the temporary one. I called it tmp. I threw all this into one big pile into the / var / www directory. Next, set the file permissions. While this version is in debug, I decided to do a generous act:
 sudo chmod 777 /var/www/index.html sudo chmod 777 /var/www/tmp sudo chmod 777 /var/www/scr.pl sudo chmod 777 /var/www/style.css 

Actually, sisiema is ready for use. But there is one thing: you will have to update with your hands. For debugging, I registered a new alias in bashrts. I called the system itself Kansatsu, which means "observation." Therefore, the alias looks like this:
alias kansatsu="sh /var/www/kansatsu.sh"


Automation


It’s no big deal to have such a powerful OS on hand and do something with your hands. Here we are rescued by the good old crowns.
 crontab -e 


As you can see, I put the update on every five minutes. Thanks to libnotify, I get reports:

The web interface is as follows:

Everything is as described: a fixed panel on the left, the rest with a margin on the right side is dynamically scaled, information is divided into blocks, row highlighting.
The project is open-source, if you want - improve, use, praise RMS.

Source: https://habr.com/ru/post/165171/


All Articles