- [Voiceover] Hi again and welcome to the next video
of section 8.
Remote Disk Usage Health Monitor.
In the previous video, we've learned about monitoring
user logins to find intruders.
In this video, we'll see how to write a monitoring script
that can connect details from remote machines in a network.
A network consists of several machines
with different users and requires centralized monitoring
of disk usage of remote machine.
The system administrator of the network needs to log
the disk usage of all the machines in the network everyday.
Each log line should contain details like the date,
IP address of the machine, device, capacity of device,
used space, free space, percentage usage, and health status.
If the disk usage of any of the partitions
in any remote machine exceeds 80%,
the health status should be set as alert
else it should be set as safe.
We need to collect the disk usage statistics
from each machine on the network individually
and write a log file in the central machine.
A script that collects the details
and writes the log can be scheduled to run everyday
at a particular time.
SSH can be used to login to remote systems
to collect disk usage data.
First, we have to setup a common user account
on all the remote machines in the network.
It's for the disk log program to login to the system.
We should configure ALTER LOGIN with SSH
for that particular user.
We assume there is a user test in all
remote machines configured with ALTER LOGIN.
Let's go through the shell script.
We can use the Cron utility to run the script
at regular intervals.
For example, to run the script everyday at 10 a.m.
write the following entry in the Crontab.
Run the command crontab - e and add the preceding line.
You can run the script manually as follows.
A sample output log for the previous scripts as follows.
In the disklog.sh script we can provide
the log file path as a command-line argument
or else it will use the default log file.
If the log file does not exist
we'll write the log file header text into the new file.
This is used to check whether the file exists or not.
The list of IP addresses of remote machines
are stored in the variable IP_LIST delimited with spaces.
It should be made sure that all the remote systems listed
in the IP_LIST have common user test
with ALTER LOGIN with SSH configured.
A for loop is used to iterate through each the IP addresses.
Our remote command df -H is executed
to get the disk free usage data using the SSH command.
It's stored in a temporary file.
A while loop is used to read the file line by line.
Data is extracted using awk and is printed.
The data is also printed.
The percentage usage is extracted using the egrep command
and percent is replaced with nothing
to get the numeric value of percent.
It's checked whether the percentage value exceeds 80.
If it is less than 80, the status is set as safe
and if greater than or equal to 80
the status is set as alert.
The entire printed data should be
redirected to the log file.
Hence the portion of the code is enclosed in a subshell
and the standard output is redirected to the log file.
Awesome, we've successfully learned
about remote disk usage health monitor.
In the next video, we'll learn how to find out
active user hours on a system.