Linux Server Information Retrieval

LinuxLinuxBeginner
Practice Now

Introduction

In this project, you will learn how to retrieve and display system information on a Linux server. The getinfo.sh script you will create can be used to quickly gather important details about the server's hardware, software, and network configuration.

👀 Preview

$ sh getinfo.sh
## Example
cpu num: 8
memory total: 30 G
memory free: 10867 M
disk size: 20G
system bit: 64
process: 40
software num: 1389
ip: 1.32.X.X

🎯 Tasks

In this project, you will learn:

  • How to retrieve the number of CPUs, total memory, available memory, disk size, system bit, number of running processes, number of installed software packages, and IP address of the server.
  • How to use various Linux commands and tools, such as grep, free, df, getconf, ps, dpkg-query, and ip, to gather system information.
  • How to write a zsh script that combines these commands to provide a comprehensive overview of the server's state.

🏆 Achievements

After completing this project, you will be able to:

  • Understand how to use shell scripting to automate the collection of system information.
  • Gain familiarity with commonly used Linux commands and their applications.
  • Create a reusable script that can be executed to quickly retrieve and display important server details.
  • Apply your newfound knowledge to monitor and troubleshoot Linux systems more effectively.

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux(("`Linux`")) -.-> linux/RemoteAccessandNetworkingGroup(["`Remote Access and Networking`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) linux/BasicFileOperationsGroup -.-> linux/wc("`Text Counting`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/InputandOutputRedirectionGroup -.-> linux/pipeline("`Data Piping`") linux/BasicSystemCommandsGroup -.-> linux/logical("`Logic Operations`") linux/TextProcessingGroup -.-> linux/grep("`Pattern Searching`") linux/TextProcessingGroup -.-> linux/sed("`Stream Editing`") linux/TextProcessingGroup -.-> linux/awk("`Text Processing`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/SystemInformationandMonitoringGroup -.-> linux/ps("`Process Displaying`") linux/SystemInformationandMonitoringGroup -.-> linux/free("`Memory Reporting`") linux/RemoteAccessandNetworkingGroup -.-> linux/ip("`IP Managing`") linux/SystemInformationandMonitoringGroup -.-> linux/df("`Disk Space Reporting`") shell/BasicSyntaxandStructureGroup -.-> shell/shebang("`Shebang`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/VariableHandlingGroup -.-> shell/variables_decl("`Variable Declaration`") shell/VariableHandlingGroup -.-> shell/variables_usage("`Variable Usage`") shell/VariableHandlingGroup -.-> shell/str_manipulation("`String Manipulation`") shell/VariableHandlingGroup -.-> shell/param_expansion("`Parameter Expansion`") shell/AdvancedScriptingConceptsGroup -.-> shell/arith_expansion("`Arithmetic Expansion`") shell/AdvancedScriptingConceptsGroup -.-> shell/cmd_substitution("`Command Substitution`") shell/AdvancedScriptingConceptsGroup -.-> shell/subshells("`Subshells and Command Groups`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") linux/FileandDirectoryManagementGroup -.-> linux/wildcard("`Wildcard Character`") subgraph Lab Skills linux/wc -.-> lab-301475{{"`Linux Server Information Retrieval`"}} linux/echo -.-> lab-301475{{"`Linux Server Information Retrieval`"}} linux/pipeline -.-> lab-301475{{"`Linux Server Information Retrieval`"}} linux/logical -.-> lab-301475{{"`Linux Server Information Retrieval`"}} linux/grep -.-> lab-301475{{"`Linux Server Information Retrieval`"}} linux/sed -.-> lab-301475{{"`Linux Server Information Retrieval`"}} linux/awk -.-> lab-301475{{"`Linux Server Information Retrieval`"}} linux/cd -.-> lab-301475{{"`Linux Server Information Retrieval`"}} linux/ps -.-> lab-301475{{"`Linux Server Information Retrieval`"}} linux/free -.-> lab-301475{{"`Linux Server Information Retrieval`"}} linux/ip -.-> lab-301475{{"`Linux Server Information Retrieval`"}} linux/df -.-> lab-301475{{"`Linux Server Information Retrieval`"}} shell/shebang -.-> lab-301475{{"`Linux Server Information Retrieval`"}} shell/comments -.-> lab-301475{{"`Linux Server Information Retrieval`"}} shell/quoting -.-> lab-301475{{"`Linux Server Information Retrieval`"}} shell/variables_decl -.-> lab-301475{{"`Linux Server Information Retrieval`"}} shell/variables_usage -.-> lab-301475{{"`Linux Server Information Retrieval`"}} shell/str_manipulation -.-> lab-301475{{"`Linux Server Information Retrieval`"}} shell/param_expansion -.-> lab-301475{{"`Linux Server Information Retrieval`"}} shell/arith_expansion -.-> lab-301475{{"`Linux Server Information Retrieval`"}} shell/cmd_substitution -.-> lab-301475{{"`Linux Server Information Retrieval`"}} shell/subshells -.-> lab-301475{{"`Linux Server Information Retrieval`"}} shell/globbing_expansion -.-> lab-301475{{"`Linux Server Information Retrieval`"}} linux/wildcard -.-> lab-301475{{"`Linux Server Information Retrieval`"}} end

Create the getinfo.sh Script

In this step, you will create the getinfo.sh script in the /home/labex/project directory.

  1. Open a text editor and create a new file named getinfo.sh in the /home/labex/project directory.

  2. Add the following shebang line at the beginning of the file:

    #!/bin/zsh

    This line specifies that the script should be executed using the zsh shell.

  3. Add the following comments to the script:

    ## getinfo.sh - Linux System Information Script
    ## This script retrieves CPU, memory, disk, and other information of a Linux server.

    These comments provide a brief description of the script's purpose.

  4. Save the file.

Retrieve CPU Information

In this step, you will add a function to the getinfo.sh script to retrieve the number of CPUs.

  1. Open the getinfo.sh script in a text editor.

  2. Add the following function to the script:

    ## Function: Retrieve CPU information
    cpu_num=$(grep -c '^processor' /proc/cpuinfo)

    This function uses the grep command to count the number of processor entries in the /proc/cpuinfo file, which represents the number of CPUs.

  3. Save the changes to the script.

Retrieve Memory Information

In this step, you will add functions to the getinfo.sh script to retrieve the total memory size and available memory size.

  1. Open the getinfo.sh script in a text editor.

  2. Add the following functions to the script:

    ## Function: Retrieve total memory size (in GB)
    memory_total=$(free -g | awk '/^Mem:/ {print $2}')
    
    ## Function: Retrieve available memory size (in MB)
    memory_free=$(free -m | awk '/^Mem:/ {print $4}')

    The first function uses the free command with the -g option to retrieve the total memory size in gigabytes. The second function uses the free command with the -m option to retrieve the available memory size in megabytes.

  3. Save the changes to the script.

Retrieve Disk Information

In this step, you will add a function to the getinfo.sh script to retrieve the total size of the file system mounted on the root directory.

  1. Open the getinfo.sh script in a text editor.

  2. Add the following function to the script:

    ## Function: Retrieve total disk size of the root filesystem (in GB)
    disk_size=$(df -h / | awk '/\// {print $2}')

    This function uses the df command with the -h option to retrieve the total size of the file system mounted on the root directory (/), and then extracts the size value using awk.

  3. Save the changes to the script.

Retrieve System Bit Information

In this step, you will add a function to the getinfo.sh script to retrieve the system bit information.

  1. Open the getinfo.sh script in a text editor.

  2. Add the following function to the script:

    ## Function: Retrieve system bit
    system_bit=$(getconf LONG_BIT)

    This function uses the getconf command to retrieve the number of bits used by the system.

  3. Save the changes to the script.

Retrieve Process Information

In this step, you will add a function to the getinfo.sh script to retrieve the number of currently running processes.

  1. Open the getinfo.sh script in a text editor.

  2. Add the following function to the script:

    ## Function: Retrieve the number of currently running processes
    process=$(ps -ef | wc -l)

    This function uses the ps command to list all running processes and then counts the number of lines using the wc command.

  3. Save the changes to the script.

Retrieve Software Information

In this step, you will add a function to the getinfo.sh script to retrieve the number of installed software packages.

  1. Open the getinfo.sh script in a text editor.

  2. Add the following function to the script:

    ## Function: Retrieve the number of installed software packages
    software_num=$(dpkg-query -f '${binary:Package}\n' -W | wc -l)

    This function uses the dpkg-query command to list all installed software packages and then counts the number of lines using the wc command.

  3. Save the changes to the script.

Retrieve IP Address Information

In this step, you will add a function to the getinfo.sh script to retrieve the IP address of the eth0 network interface.

  1. Open the getinfo.sh script in a text editor.

  2. Add the following function to the script:

    ## Function: Retrieve the IP address of eth0
    ip=$(ip addr show eth0 | awk '/inet / {print $2}' | sed 's|/.*||')

    This function uses the ip command to retrieve the IP address of the eth0 network interface and then extracts the IP address using awk and sed.

  3. Save the changes to the script.

Output the System Information

In this final step, you will add the output section to the getinfo.sh script to display the system information.

  1. Open the getinfo.sh script in a text editor.

  2. Add the following output section to the script:

    ## Output information
    echo "cpu num: $cpu_num"
    echo "memory total: $memory_total G"
    echo "memory free: $memory_free M"
    echo "disk size: $disk_size"
    echo "system bit: $system_bit"
    echo "process: $((process - 1))"
    echo "software num: $software_num"
    echo "ip: $ip"

    This section uses the echo command to print the system information in the required format.

  3. Save the changes to the script.

Congratulations! You have now completed the getinfo.sh script. You can execute the script by running the following command in the terminal:

sh getinfo.sh

The script will output the system information as specified in the project requirements.

## Example
cpu num: 8
memory total: 30 G
memory free: 10867 M
disk size: 20G
system bit: 64
process: 40
software num: 1389
ip: 1.32.X.X

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Linux Tutorials you may like