0% found this document useful (0 votes)
34 views5 pages

Linux System Monitoring Script Guide

The document provides documentation for a system monitoring script including installation instructions, usage examples, design considerations, test results, and conclusions. The bash script monitors various system metrics and outputs logs and HTML reports. It utilizes modular functions and logging for flexibility and maintainability.

Uploaded by

djcqicr057
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views5 pages

Linux System Monitoring Script Guide

The document provides documentation for a system monitoring script including installation instructions, usage examples, design considerations, test results, and conclusions. The bash script monitors various system metrics and outputs logs and HTML reports. It utilizes modular functions and logging for flexibility and maintainability.

Uploaded by

djcqicr057
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

System Monitoring Script Documentation

a) Installation and Usage

Prerequisites:

● A Linux system with a bash shell.


● The following standard Unix commands installed: free, atq, lscpu, df, uptime, find, and
dpkg. You can verify their installation using command -v <command_name>. If any are
missing, use your distribution's package manager to install them (e.g., apt install
<package_name> on Debian/Ubuntu).

Installation:

● Save the Script: Save the script content as system_monitor.sh. You can use a text editor
of your choice.
● Make it Executable: Open a terminal window and navigate to the directory where you
saved the script using the cd command. Then, grant the script execution permission
using the following command:

chmod +x system_monitor.sh

Usage:

Run the Script: Execute the script from the terminal with the following command:

./system_monitor.sh

This will run the script with the default log level (standard).

Specify a Log Level (Optional): The script supports four log levels: verbose, debug, standard,
and key-details. You can specify a desired log level when running the script using the following
syntax:

./system_monitor.sh -l <log_level>

Replace <log_level> with your preferred level (verbose, debug, standard, or key-details). For
example:

./system_monitor.sh -l debug

This will run the script with the debug log level.

Output:

The script will generate two files: [Link] that contains log messages based on the chosen
log level and [Link] which is a HTML file presents the logged information in a user-friendly
format for viewing in a web browser.

b) Design consideration - what commands you used, how you constructed your script(s)

Shebang and Script Execution: The script begins with a shebang line #!/bin/bash,
indicating the interpreter required to execute the script (Bash shell).

Variable Declarations: The script utilizes variables to store configuration details like the log file
path (LOG_FILE) and default log level (LOG_LEVEL). This improves readability and
maintainability.

Modular Functions for Specific Tasks: The script employs well-defined functions for various
monitoring activities:

● get_datetime(): Retrieves the current date and time.


● log_info(): Logs messages to the designated file with appropriate formatting based
on the chosen log level.
● monitor_* functions: Each function focuses on monitoring a specific system aspect like
memory usage, scheduled tasks, hardware information, system logs, system load, file
changes, and installed packages.
● generate_html_output(): Creates an HTML file presenting the logged information.
● main(): Orchestrates the execution of all monitoring functions and HTML output
generation.

Command-Line Argument Parsing for Flexibility: The script leverages getopts to parse
command-line arguments, allowing users to specify the log level (-l). This empowers them to
control the detail level of logged information (verbose, debug, standard, or key-details).

Justification for Design Choices:

● Bash Scripting: Bash is a prevalent scripting language pre-installed on most Linux


distributions, making the script portable and readily executable.
● Modular Design: Separating functionalities into functions promotes code reusability,
simplifies maintenance, and facilitates testing/debugging of individual components.
● Persistent Logging: Logging data to a file ([Link]) enables centralized storage
and historical analysis of system monitoring information.
● Configurable Log Levels: The script offers configurable log levels, granting users
control over the amount of detail logged, and enhancing its adaptability to various needs.
● HTML Output for User-Friendliness: Generating an HTML report ([Link])
provides a user-friendly format for viewing system monitoring data in a web browser.
● Standard Unix Commands: The script utilizes widely available and well-documented
Unix commands for system information gathering, ensuring compatibility across diverse
Unix-like systems.
c) Test Results

The script generates two types of output: a log file ([Link]) and an HTML file ([Link]).
The log file contains the monitoring information logged by the script, while the HTML file
presents the log output in a web browser-friendly format.

The log level specified when running the script will determine the verbosity of the log output.

Here's an example of what the log file might contain:

Log file ([Link], standard log level):

HTML File Output ([Link]):


The HTML file will contain the same log output as the log file, but enclosed within HTML tags for
easier viewing in a web browser. The output will be wrapped in <pre> tags to preserve the
formatting.
Example HTML output:
d) Conclusion/Reflection

Advantages:

Cross-Platform Compatibility: Leveraging Bash ensures seamless execution across Linux


and macOS environments, enhancing its applicability.

Modular Design: The script's modular structure promotes maintainability, reusability, and
efficient troubleshooting.

Configurable Logging: Users can tailor the logging verbosity to their needs, enabling focused
analysis.

Standard Unix Commands: By employing well-documented and ubiquitous Unix commands,


the script minimizes external dependencies.

HTML Output: The script generates HTML reports, facilitating data visualization in web
browsers and fostering collaboration.

Scheduling Automation: Integration with cron allows for automated, periodic monitoring,
eliminating manual intervention.

Disadvantages:

Functionality: While comprehensive, the script might require additional tools or features for in-
depth monitoring of specific system aspects.

Command-Line Interface: The script operates solely in a command-line environment, which


may be less intuitive for GUI-oriented users.

Error Handling: The script's error handling capabilities could be enhanced to ensure more
robust operation in diverse scenarios.

Platform Nuances: While leveraging standard commands, minor variations across Unix-like
systems might necessitate script adjustments.

Scalability: Designed for single-system monitoring, it may not be optimal for large-scale
deployments. Additional solutions might be needed.

Data Analysis/Visualization: The script primarily focuses on log and HTML output. Advanced
data analysis or visualizations might require further processing or external tools.

e) Bibliography
For Bash Scripting:
Bash Guide for Beginners. Available at [Link]
tutorial-linux-shell-script-and-command-line-for-beginners/
Advanced Bash Scripting Guide. Available at [Link]

For System Monitoring Concepts:


[Link]

For HTML Basics:


HTML Tutorial. Available at [Link]
Bash Guide: [Link] - A comprehensive guide to the
Bash scripting language, covering basic syntax, control flow, functions, and more.
Advanced Bash Scripting Guide: [Link] - A more in-depth resource
that delves into advanced topics like parameter parsing, process management, and working
with files.

Linux Shell Scripting Tutorial: [Link]


scripting/[Link] - A tutorial with examples that covers basic and intermediate concepts of
shell scripting.
The Shell Scripting Tutorial: [Link] - Another interactive tutorial that
introduces scripting concepts through exercises.
Stack Overflow: [Link] - A great resource for
finding solutions and asking questions related to Linux scripting.
"The Linux Command Line" by William Shotts - A classic book that covers not only basic shell
commands but also scripting fundamentals.
"Learning Bash" by Cameron Newham - An introductory book that teaches scripting concepts
through practical examples.
"Automate the Boring Stuff with Python" by Al Sweigart (While this book focuses on Python, it
introduces scripting concepts applicable to Bash as well)

Common questions

Powered by AI

Using standard Unix commands contributes significantly to cross-platform compatibility because these commands, such as free and lscpu, are widely supported across Unix-like systems. This compatibility reduces reliance on non-standard packages, promoting portability. However, variations in command output or options between systems could necessitate script adjustments, slightly affecting the uniformity of operations across different platforms .

Users unfamiliar with command-line environments might struggle with using the script due to its lack of graphical interface, requiring them to understand shell commands for installation and execution. Issues such as improper permissions, missing dependencies (commands not found), and understanding log levels might occur, posing a learning curve for GUI-oriented users. This environment might need clearer documentation or additional tools to make it more accessible .

Command-line argument parsing in the System Monitoring Script allows users to specify the log level using flag inputs. Implemented with getopts, it enables flexibility by parsing options like -l to set the desired log verbosity (verbose, debug, standard, key-details). This approach empowers users to control the detail level of logged information, making the script adaptable to varying needs .

A modular design is beneficial as it promotes code reusability, simplifies maintenance, and makes debugging easier by isolating individual script components. Each function handles specific tasks, allowing developers to modify or test parts of the script without affecting others. However, while this design supports maintainability, the script's focus on a single system means scalability to larger deployments might require significant restructuring or additional tools to handle complexities of distributed environments .

The script ensures portability by using Bash scripting, which is pre-installed on most Linux distributions, making it readily executable across different systems. It also relies on standard Unix commands like free, df, and lscpu, which are widely available and documented, thereby minimizing compatibility issues .

The main components of the System Monitoring Script are: shebang and script execution, variable declarations, modular functions, command-line argument parsing, and output generation. The shebang line (#!/bin/bash) specifies the interpreter for executing the script. Variable declarations store configuration details like log file paths, enhancing readability. Modular functions are used for specific tasks, such as get_datetime() for date/time retrieval and monitor_* for different system aspects monitoring like memory and hardware. Command-line argument parsing via getopts enables flexible input handling for configuration, like log level. Output generation involves a log file (output.log) and an HTML file (output.html) for recording and presenting system information .

The HTML output feature provides several advantages. It enhances user-friendliness by allowing users to view system monitoring data in a web browser, which can facilitate data visualization and collaboration. This format is particularly useful for users who prefer graphical representations over text logs. However, the limitations include its focus on basic log presentations wrapped in HTML, possibly requiring further processing for advanced data analysis or presentation needs that demand external tools .

Persistent logging to a file allows historical analysis and centralized storage of system data, which is essential for tracking trends and diagnosing issues over time. Configurable log levels provide control over the amount of detail logged, enabling users to choose verbosity based on their analysis needs or storage constraints. These features enhance the script's adaptability to diverse scenarios, from routine monitoring to in-depth system diagnostics .

Using a cron job for the script facilitates automated periodic monitoring by scheduling regular execution without manual intervention. This automation ensures consistent data collection and timely alerts on system performance. However, limitations include the cron job's reliance on the system's cron scheduler, which might face issues like misconfigured crontab or system policies affecting the execution environment .

The current script lacks comprehensive error handling, which could be improved by implementing strategies to catch and respond to errors during execution, such as using conditional statements to verify command success and logging error messages. Enhancing error handling would make the script more robust, especially in diverse or unexpected scenarios, ensuring more reliable operation .

You might also like