Today we will be trying to do the system monitoring of our Linux system. Once this system monitoring is implemented in our local machine successfully we will be moving this to our ec2 instance in AWS and setting the cron job.
What we will be monitoring in our system?
Disk Usage
Memory Usage
CPU usage
The command for CPU usage: cpu_usage.sh
top
#or
cat /proc/cpuinfo
#or
ps -eocomm,pcpu | grep -E -v '(0.0)'
The command for memory usage: mem_usage.sh
ps -eocomm,pmem | grep -E -v '(0.0)'
The command for disk usage: disk_usage.sh
df -h
We will be creating a main.sh file where we will source all the above files (i.e. CPU, mem, and disk). In this position, we will be giving the option to choose what the user wants to check either CPU usage or disk usage or memory usage, or all. Using the case statement the user will be able to achieve what parameter they want to check.
In this project, we will be performing different file operations:
creating files
checking whether the file is present or not
listing all files and directories
removing the files once the task is done
On executing this, we will be generating a file as below.
The generated file is in the .txt extension. We will be converting it to the PDF using cupsfilter.
cupsfilter "$output_file".txt >"$output_file".pdf
The pdf file will be emailed to the user and once the file is sent we will remove these files from the system.
In this project, the password is generated for sending the email. For generating the password we use this link: https://myaccount.google.com/apppasswords
The email sent is successfully done.
Now we will copy these files to our ec2 instance and set the cron job.
Creating an EC2 instance
Copying files to ec2 instance
Now creating the cron job for this script.
crontab -e
We have set the cronjob successfully. The script will be executed every hour and the report will be generated.
Since I have mentioned some basic code and some snippets of the project, you can try it yourself using the following repo link: