Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save L3onSW/0c5dcfe424baabd4819cedf3a37e1cc5 to your computer and use it in GitHub Desktop.
Save L3onSW/0c5dcfe424baabd4819cedf3a37e1cc5 to your computer and use it in GitHub Desktop.

UbuntuまたはCentOSのサーバの情報を表示する

UbuntuまたはCentOSのサーバの情報を表示するための簡単なシェルスクリプト例は以下の通り。
ただし、閲覧するファイルが存在しない場合の分岐処理などは書いていない簡単なものであることに注意されたい。

#!/bin/bash
# ======================================================================
# Display information about the currently used server
# (Can work on Ubuntu, CentOS, cannot be used on MacOS...)
#
# How to run: ./view_server_info.sh
#
# Created on 2024/01/21, author: L3onSW
# ======================================================================

# ----------------------------------------------------------------------
# Use the following files
# ----------------------------------------------------------------------
hostnameinfo=/proc/sys/kernel/hostname
osinfo=/etc/os-release
cpuinfo=/proc/cpuinfo
memoryinfo=/proc/meminfo

# ----------------------------------------------------------------------
# Get hostname
# ----------------------------------------------------------------------
hostname="$(cat ${hostnameinfo})"
echo "Hostname: ${hostname}"

# ----------------------------------------------------------------------
# Get OS distribution name and version number
# ----------------------------------------------------------------------
. ${osinfo}
OS=${NAME}
OS_version=${VERSION_ID}
echo "OS: ${OS} ${OS_version}"

# ----------------------------------------------------------------------
# Get CPU's model name
# ----------------------------------------------------------------------
cpu=$(awk -F: '/^model name/{print $2; exit}' < "${cpuinfo}")
# Remove unnecessary characters such as (R)
cpu=$(sed -e 's,(R),,g' -e 's,(TM),,g' -e 's,  *, ,g' -e 's,^ ,,' <<< "${cpu}")
echo "CPU: ${cpu}"

# ----------------------------------------------------------------------
# Get number of physical processors
# ----------------------------------------------------------------------
physical_processors=$(grep '^physical id' < "${cpuinfo}" | sort -u | wc -l)
cpu="${physical_processors} ${cpu}"

# ----------------------------------------------------------------------
# Get number of cores per cpu
# ----------------------------------------------------------------------
cores_per_cpu=$(awk '/^cpu cores/{print $4; exit}' < "${cpuinfo}")

# ----------------------------------------------------------------------
# Get number of total-cores
# ----------------------------------------------------------------------
cores=$((physical_processors*cores_per_cpu))
echo "Cores: ${cores} (= ${cores_per_cpu} cores * ${physical_processors} physical processors)" 

# ----------------------------------------------------------------------
# Get number of logical processors
# ----------------------------------------------------------------------
logical_processors=$(awk '/^processor/{n++} END{print n}' < "${cpuinfo}")
echo "Number of logical processors: ${logical_processors} (if hyper-threading, then \"Cores * 2\")"

# ----------------------------------------------------------------------
# Get total physical memory size recognized by kernel
# ----------------------------------------------------------------------
total_memory_kB=$(awk -F: '/^MemTotal/{print $2; exit}' < "${memoryinfo}")
total_memory_kB=$(sed 's/^[ \t]*//' <<< "${total_memory_kB}")  # Remove leading whitespace
total_memory_kB=$(sed 's, kB,,g' <<< "${total_memory_kB}")
total_memory_GB=$((total_memory_kB/(1000*1000)))
echo "Total memory size: ${total_memory_kB} kB = ${total_memory_GB} GB"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment