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.

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
âœĻ Check Solution and Practice

Summary

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

Other Linux Tutorials you may like