Creating A GNU/Linux Bash Script For Monitoring And Logging System Activity
Specification
This report has been focused on developing a change logger script created in Ubuntu OS and managing of regular activity of user utilizing this system. Therefore, for creating shell script if the system, it is extracted from different shell command and output has been stored in the HTML file. IT would help in monitoring the system in a proper manner. The pre-installed command line tools for utilizing creation of shell script and output has been stored in a temporary log file for managing changes in the logs.
The shell script has been created using nano editor and named as “mySysMonitor.sh”, and proper permission has been added to the shell script for running it. Administrative permission has been required for running the shell script and “chmod +x mySysMonitor.sh” command has been utilized for giving appropriate permission and running shell script. “sudo ./mySysMonitor.sh”, command has been used for running the shell script as root user and generate the output in a HTML file.
The shell script has been designed for separating command for obtaining information and data for the development of change logger script. The prior function of the script has been monitoring current computer system and activity of user in order to develop log with a key. The script named “mySysMonitor.sh” has been automatically written in the log file named “mySysMonitor.log”. The output of the shell script has been stored in the HTML file and utilize “mySysMonitor.log” as temporary file for storing data and result of the command. The contents if the file has been copied in a temporary file and tail-f command has been used for comparing changes in the log file and identification has been made in the current system. For constructing the shell script the following commands are used such as:
1) Current Processes
echo “**********Current Process**********
$(ps -aux | sort -rk 3,3 | head -n 6)” >>mySysMonitor.log
current_processes_pid=`ps -ef | awk {‘print$2′} | cut -f2 -d’-‘`
current_processes_time=`ps -ef | awk {‘print$5′} | cut -f2 -d’-‘`
current_processes_process=`ps -ef | awk {‘print$8′} | cut -f2 -d’-‘`
2) Currently Logged in Users
echo ” **********Currently Logged in Users**********
$(who)” >>mySysMonitor.log
current_loggedin_user_name=`who | awk {‘print$1′} | cut -f1 -d’,’`
current_loggedin_user_date=`who | awk {‘print$3′} | cut -f1 -d’,’`
current_loggedin_user_time=`who | awk {‘print$4′} | cut -f1 -d’,’`
3) Plugged in Devices
echo ” **********Plugged in Devices**********
$(ls -l)” >>mySysMonitor.log
device_plugged_in_permissions=`ls -l /dev/ | awk {‘print$1’}`
device_plugged_in_UID=`ls -l /dev/ | awk {‘print$3’}`
device_plugged_in_number=`ls -l /dev/ | awk {‘print$6’}`
device_plugged_in_month=`ls -l /dev/ | awk {‘print$7’}`
device_plugged_in_date=`ls -l /dev/ | awk {‘print$8’}`
device_plugged_in_time=`ls -l /dev/ | awk {‘print$9’}`
device_plugged_in_disk=`ls -l /dev/ | awk {‘print$10’}`
4) Disk Usage
echo “**********Disk Usage**********
$(du -sh)” >>mySysMonitor.log
overall_disk_usage=`du -sh | tail -l | awk {‘print$1’}`
device_plugged_in_number=`ls -l /dev/ | awk {‘print$6’}`
home_directory_disk_usage=`du -sh /home7/P17245067 | tail -l | awk {‘print$1’}`
other_directory_disk_usage=`du -sh /home7/P17245067/Downloads | tail -l | awk {‘print$1’}`
5)Network Interfaces
echo “**********Network Interfaces**********
$(ip -s link)” >>mySysMonitor.log
network_interfaces_status=`ip -s link`
6)RAM Usage
echo “**********RAM Usage**********
Design Consideration
$(free -hm)” >>mySysMonitor.log
ram_usage=`free -hm | head -2 | tail -1 | awk {‘print$3’}`
ram_total=`free -hm | head -2 | tail -1 | awk {‘print$2’}`
7) Current Operating System Version
echo “**********Current Operating System Version**********
$(uname -r)” >>mySysMonitor.log
Current_os=`uname -r `
The shell script prepared for management of the change log is given below:
#! /bin/bash
#Author : – Kanan Patel
#Declaring variables
#set -x
1) Current Processes
echo “**********Current Process**********
$(ps -aux | sort -rk 3,3 | head -n 6)” >>mySysMonitor.log
current_processes_pid=`ps -ef | awk {‘print$2′} | cut -f2 -d’-‘`
current_processes_time=`ps -ef | awk {‘print$5′} | cut -f2 -d’-‘`
current_processes_process=`ps -ef | awk {‘print$8′} | cut -f2 -d’-‘`
2) Currently Logged in Users
echo ” **********Currently Logged in Users**********
$(who)” >>mySysMonitor.log
current_loggedin_user_name=`who | awk {‘print$1′} | cut -f1 -d’,’`
current_loggedin_user_date=`who | awk {‘print$3′} | cut -f1 -d’,’`
current_loggedin_user_time=`who | awk {‘print$4′} | cut -f1 -d’,’`
3) Plugged in Devices
echo ” **********Plugged in Devices**********
$(ls -l)” >>mySysMonitor.log
device_plugged_in_permissions=`ls -l /dev/ | awk {‘print$1’}`
device_plugged_in_UID=`ls -l /dev/ | awk {‘print$3’}`
device_plugged_in_number=`ls -l /dev/ | awk {‘print$6’}`
device_plugged_in_month=`ls -l /dev/ | awk {‘print$7’}`
device_plugged_in_date=`ls -l /dev/ | awk {‘print$8’}`
device_plugged_in_time=`ls -l /dev/ | awk {‘print$9’}`
device_plugged_in_disk=`ls -l /dev/ | awk {‘print$10’}`
4) Disk Usage
echo “**********Disk Usage**********
$(du -sh)” >>mySysMonitor.log
overall_disk_usage=`du -sh | tail -l | awk {‘print$1’}`
device_plugged_in_number=`ls -l /dev/ | awk {‘print$6’}`
home_directory_disk_usage=`du -sh /home7/P17245067 | tail -l | awk {‘print$1’}`
other_directory_disk_usage=`du -sh /home7/P17245067/Downloads | tail -l | awk {‘print$1’}`
5) Network Interfaces
echo “**********Network Interfaces**********
$(ip -s link)” >>mySysMonitor.log
network_interfaces_status=`ip -s link`
6)RAM Usage
echo “**********RAM Usage**********
$(free -hm)” >>mySysMonitor.log
ram_usage=`free -hm | head -2 | tail -1 | awk {‘print$3’}`
ram_total=`free -hm | head -2 | tail -1 | awk {‘print$2’}`
7)Current Operating System Version
echo “**********Current Operating System Version**********
$(uname -r)” >>mySysMonitor.log
Current_os=`uname -r `
Creating a directory if it doesn’t exist to store reports first, for easy maintenance.
if [ ! -d ${HOME}/Linux_System_Monitor ]
then
mkdir ${HOME}/Linux_System_Monitor
fi
html=”${HOME}/Linux_System_Monitor/Linux-System-Monitor-`hostname`-`date +%y%m%d`-`date +%H%M`.html”
Generating HTML file
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “
” >> $html
echo “
” >> $html
echo “
” >> $html
echo “
” >> $html
echo “” >> $html
echo “
- ” >> $html
echo “
- ” >> $html
echo “Current Processes” >> $html
echo “” >> $html
echo “
- ” >> $html
echo “Logged in Users” >> $html
echo “” >> $html
echo “
- ” >> $html
echo “Plugged in Devices” >> $html
echo “” >> $html
echo “
- ” >> $html
echo “Disk Usage” >> $html
echo “” >> $html
echo “
- ” >> $html
Extensive Test Result and Exemplary Log and Screen Output
echo “Networks” >> $html
echo “” >> $html
echo “
- ” >> $html
echo “Ram Usage” >> $html
echo “” >> $html
echo “
- ” >> $html
echo “Current Operating System Version” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “
” >> $html
echo “
” >> $html
echo “
” >> $html
echo “
” >> $html
echo “” >> $html
echo “” >> $html
echo “PID” >> $html
echo “Starting Time” >> $html
echo “Processes” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “$current_processes_pid” >> $html
echo “$current_processes_time” >> $html
echo “$current_processes_time” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “
” >> $html
echo “
” >> $html
echo “
” >> $html
echo “
” >> $html
echo “” >> $html
echo “” >> $html
echo “Username” >> $html
echo “From” >> $html
echo “Login Time” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “$current_loggedin_user_name” >> $html
echo “$current_loggedin_user_date” >> $html
echo “$current_loggedin_user_time” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “
” >> $html
echo “
” >> $html
echo “
” >> $html
echo “” >> $html
echo “” >> $html
echo “Permissions” >> $html
echo “UID” >> $html
echo “Disk Number” >> $html
echo “Month” >> $html
echo “Date” >> $html
echo “time” >> $html
echo “Disk Name” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “$device_plugged_in_permissions” >> $html
echo “$device_plugged_in_UID” >> $html
echo “$device_plugged_in_number” >> $html
echo “$device_plugged_in_month” >> $html
echo “$device_plugged_in_date” >> $html
echo “$device_plugged_in_time” >> $html
echo “$device_plugged_in_disk” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “
” >> $html
echo “
” >> $html
echo “
” >> $html
echo “” >> $html
echo “” >> $html
echo “Overall Disk Size” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “$overall_disk_usage” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “
” >> $html
echo “
” >> $html
echo “” >> $html
echo “” >> $html
echo “Home Directory Size” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “$home_directory_disk_usage” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “
” >> $html
echo “
” >> $html
echo “” >> $html
echo “” >> $html
echo “Other Directory Disk Size” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “$other_directory_disk_usage” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “
” >> $html
echo “
” >> $html
echo “
$network_interfaces_status
” >> $html
echo “” >> $html
echo “
” >> $html
echo “
” >> $html
echo “
” >> $html
echo “” >> $html
echo “” >> $html
echo “Used RAM Size” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “$ram_usage” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “Total RAM Size” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “$ram_total” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “
” >> $html
echo “
Current Operating System Version
” >> $html
echo “
$Current_os
” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
echo “” >> $html
Conclusion
The creation of shell script helps in reducing effort of the network engineer for monitoring and managing activity of user. The shell script has been executed for getting all information about the current system with the help of using a single command. There are some disadvantages as the speed is slower when the shell script is compared with several programming language for monitoring the activity of current user and managing changes made in the system.
Berlich, R., Hug, S., Ottmann, R., Kehbel, H., Hacker, S. and Mikulla, R., 2017, January. Seamless Integration of Docker-based Applications into Linux Servers. In International Symposium on Grids and Clouds (ISGC) 2016 (Vol. 270, p. 029). SISSA Medialab.
Cannon, J., 2015. Shell Scripting: How to Automate Command Line Tasks Using Bash Scripting and Shell Programming.
Flynt, C., Lakshman, S. and Tushar, S., 2017. Linux Shell Scripting Cookbook. Packt Publishing Ltd.
Harvey, S., 2017. Linux: Beginners guide for learning Linux & Shell scripting.
Hashmi, M.F., Kumar, M.P. and Rao, K.S., 2017. A Framework for Optimization of the Boot Time on Embedded Linux Environment with Raspberry Pi Platform. International Journal of Computer Applications, 162(3).
Johnson, S., 2018. The Complete LINUX Operating system for Absolute Beginners Guide: Also Learn UNIX Administration, Shell Scripting, Virtual Terminal and its application.
Kochan, S.G. and Wood, P., 2016. Shell Programming in Unix, Linux and OS X: The Fourth Edition of Unix Shell Programming. Addison-Wesley Professional.
Naik, G., 2018. Learning Linux Shell Scripting: Leverage the power of shell scripts to solve real-world problems.
Rana, A. and Parekh, C., 2017. A Review Paper on Linux Mitigation & Countermeasures Against It’s Vulnerabilities.
Rocha, L.A., 2017. INL (Is Not Linux): Challenges of Building a New FOSS Operating System. International Journal on Data Science and Technology, 3(1), p.8.
Uitto, J., Rauti, S., Mäkelä, J.M. and Leppänen, V., 2015, October. Preventing malicious attacks by diversifying Linux shell commands. In SPLST (pp. 206-220).