【小技巧分享】通过脚本快速获取CentOS7服务器基本系统信息

简介: 【小技巧分享】通过脚本快速获取CentOS7服务器基本系统信息

通过脚本快速获取CentOS7服务器基本系统信息

在github上找到两个快速获取CentOS7服务器基本系统信息的脚本,比较好使,分享给大家


1、monitor_system.sh


./monitor_system.sh


效果如下

640.jpg

其中获取公网IP的方法


curl -s https://api.myip.la



640.jpg

脚本如下


#!/bin/bash
echo "==============================================================================="
if ! wget -q --spider http://baidu.com
then
        echo  "Internet:  Disconnected"
else
        echo  "Internet:  Connected"
fi
# Check OS Release Version and Name
echo "==============================================================================="
name=$(</etc/os-release grep 'PRETTY_NAME=' | cut -f2 -d\")
echo "OS :" "$name"
# Check Architecture
echo "==============================================================================="
architecture=$(uname -m)
echo "Architecture :"  "$architecture"
# Check Kernel Release
echo "==============================================================================="
kernelrelease=$(uname -r)
echo "Kernel Release :"  "$kernelrelease"
# Check Hostname
echo "==============================================================================="
hostname=$(hostname)
echo "Hostname :"  "$hostname"
# Check Internal IP
echo "==============================================================================="
internalip=$(hostname -I)
echo "Internal IP :"  "$internalip"
# Check External IP
echo "==============================================================================="
externalip=$(curl -s https://api.myip.la;echo)
echo "External IP :" "$externalip"
# Check DNS
echo "==============================================================================="
nameservers=$(</etc/resolv.conf grep "nameserver" | awk '{print $2}')
echo "Name Servers :"
echo "$nameservers"
# Check Logged In Users
echo "==============================================================================="
echo "Logged In users :"
who
# Check RAM and SWAP Usages
echo "==============================================================================="
echo "Memory usage :"
free -h 
# Check Disk Usage
echo "==============================================================================="
df -h| grep 'Filesystem\|/dev/sda*\|/dev/mapper*\|/dev/md*\|/dev/disk*\|/dev/vda*\|/dev/dm*' >> "$tdir/diskusage.tmp"
echo "Disk Usage :"
cat "$tdir/diskusage.tmp"
# Check Load Average
echo "==============================================================================="
loadaverage=$(</proc/loadavg awk '{print $1,$2,$3}')
echo "Load Average :"  "$loadaverage"
# Check iowait
echo "==============================================================================="
iowait=$(iostat -c|awk '/^ /{print $4}')
echo "I/O in %:" "$iowait"
# Check System Uptime
echo "==============================================================================="
uptime=$(uptime | awk '{print $3,$4}' | cut -f1 -d,)
echo "System Uptime Days/(HH:MM) :"  "$uptime"
echo "==============================================================================="


2、system_info.sh


./system_info.sh


效果如下

640.jpg


由于无wget命令,导致无法获取公网IP信息

需要手动yum install wget,安装wget命令

效果如下


640.jpg


脚本如下


#!/usr/bin/env bash
#
# Author:       Seaton Jiang <seaton@vtrois.com>
# Github URL:   https://github.com/vtrois/spacepack
# License:      MIT
# Date:         2020-08-13
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
RGB_DANGER='\033[31;1m'
RGB_WAIT='\033[37;2m'
RGB_SUCCESS='\033[32m'
RGB_WARNING='\033[33;1m'
RGB_INFO='\033[36;1m'
RGB_END='\033[0m'
tool_info() {
    echo -e "========================================================================================="
    echo -e "                             System Info tool for SpacePack                              "
    echo -e "          For more information please visit https://github.com/vtrois/spacepack          "
    echo -e "========================================================================================="
}
operation_system() {
    [ -f /etc/redhat-release ] && awk '{print ($1,$3~/^[0-9]/?$3:$4)}' /etc/redhat-release && return
    [ -f /etc/os-release ] && awk -F'[= "]' '/PRETTY_NAME/{print $3,$4,$5}' /etc/os-release && return
    [ -f /etc/lsb-release ] && awk -F'[="]+' '/DESCRIPTION/{print $2}' /etc/lsb-release && return
}
public_ipv4() {
    local IP=$( wget -qO- -t1 -T2 ipv4.icanhazip.com )
    [ -z ${IP} ] && IP=$( wget -qO- -t1 -T2 ipinfo.io/ip)
    [ ! -z "${IP}" ] && echo ${IP} || echo -e "${RGB_DANGER}Unknown${RGB_END}"
}
metadata_tencent() {
    IPV4=$( wget -qO- -t1 -T2 metadata.tencentyun.com/latest/meta-data/public-ipv4 )
    LOCALIP=$( wget -qO- -t1 -T2 metadata.tencentyun.com/latest/meta-data/local-ipv4 )
    MACADDRESS=$( wget -qO- -t1 -T2 metadata.tencentyun.com/latest/meta-data/mac )
    INSTANCEID=$( wget -qO- -t1 -T2 metadata.tencentyun.com/latest/meta-data/instance-id )
    INSTANCENAME=$( wget -qO- -t1 -T2 metadata.tencentyun.com/latest/meta-data/instance-name )
    UUID=$( wget -qO- -t1 -T2 metadata.tencentyun.com/latest/meta-data/uuid )
    REGIONZONE=$( wget -qO- -t1 -T2 metadata.tencentyun.com/latest/meta-data/placement/zone )
    CHARGETYPE=$( wget -qO- -t1 -T2 metadata.tencentyun.com/latest/meta-data/payment/charge-type )
    CREATETIME=$( wget -qO- -t1 -T2 metadata.tencentyun.com/latest/meta-data/payment/create-time )
    TERMINATIONTIME=$( wget -qO- -t1 -T2 metadata.tencentyun.com/latest/meta-data/payment/termination-time )
}
metadata_ali(){
    IPV4=$( wget -qO- -t1 -T2 100.100.100.200/latest/meta-data/eipv4 )
    LOCALIP=$( wget -qO- -t1 -T2 100.100.100.200/latest/meta-data/private-ipv4 )
    MACADDRESS=$( wget -qO- -t1 -T2 100.100.100.200/latest/meta-data/mac )
    INSTANCEID=$( wget -qO- -t1 -T2 100.100.100.200/latest/meta-data/instance-id )
    INSTANCENAME=$( wget -qO- -t1 -T2 100.100.100.200/latest/meta-data/hostname )
    INSTANCETYPE=$( wget -qO- -t1 -T2 100.100.100.200/latest/meta-data/instance/instance-type )
    UUID=$( wget -qO- -t1 -T2 100.100.100.200/latest/meta-data/serial-number )
    REGIONZONE=$( wget -qO- -t1 -T2 100.100.100.200/latest/meta-data/zone-id )
}
MEMTOTAL=$( cat /proc/meminfo | grep "MemTotal" | awk -F" " '{total=$2/1000}{printf("%d MB",total)}' )
MEMFREE=$( cat /proc/meminfo | grep "MemFree" | awk -F" " '{free=$2/1000}{printf("%d MB",free)}' )
SWAPTOTAL=$( cat /proc/meminfo  | grep "SwapTotal" | awk -F" " '{total=$2/1000}{printf("%d MB",total)}' )
SWAPFREE=$( cat /proc/meminfo  | grep "SwapFree" | awk -F" " '{free=$2/1000}{printf("%d MB",free)}' )
CPUMODEL=$( cat /proc/cpuinfo | grep "model name" | awk 'END{print}' | awk -F": " '{print $2}' )
CPUMHZ=$( cat /proc/cpuinfo | grep "cpu MHz" | awk 'END{print}' | awk -F": " '{print($2,"MHz")}' )
CPUCORES=$( cat /proc/cpuinfo | awk -F: '/model name/ {core++} END {print core}' )
CPUCACHE=$( cat /proc/cpuinfo | grep "cache size" | awk 'END{print}' | awk -F": " '{print $2}' )
SYSOS=$( operation_system )
SYSRISC=$( uname -m )
SYSLBIT=$( getconf LONG_BIT )
KERNEVERSIONL=$( cat /proc/version | awk -F" " '{print $3}' )
IPV6=$( ifconfig | grep "inet6" | grep -v "fe80\|::1" | awk -F" " '{print $2}' )
NAMESERVER=$( cat /etc/resolv.conf | awk '/^nameserver/{print $2}' | awk 'BEGIN{FS="\n";RS="";ORS=""}{for(x=1;x<=NF;x++){print $x"\t"} print "\n"}' )
TENCENTCLOUD=$( wget -qO- -t1 -T2 metadata.tencentyun.com )
ALICLOUD=$( wget -qO- -t1 -T2 100.100.100.200 )
if [ ! -z "${TENCENTCLOUD}" ]; then
metadata_tencent
elif [ ! -z "${ALICLOUD}" ]; then
metadata_ali
else
IPV4=$( public_ipv4 )
LOCALIP=$( ifconfig | grep "inet" | grep -v "127.0" | xargs | awk -F '[ :]' '{print $2}' )
MACADDRESS=$( ifconfig | grep "ether" | awk -F" " '{print $2}' )
fi
clear
tool_info
echo -e "\n${RGB_WARNING}Hardware Overview (Contains the System, CPU and Memory)${RGB_END}"
echo -e "${RGB_INFO}Operation System       ${RGB_END}: ${SYSOS}"
echo -e "${RGB_INFO}Hardware Types         ${RGB_END}: ${SYSRISC} (${SYSLBIT} Bit)"
echo -e "${RGB_INFO}Kernel Version         ${RGB_END}: ${KERNEVERSIONL}"
echo -e "${RGB_INFO}CPU model              ${RGB_END}: ${CPUMODEL}"
echo -e "${RGB_INFO}CPU Cores              ${RGB_END}: ${CPUCORES}"
echo -e "${RGB_INFO}CPU Cache Size         ${RGB_END}: ${CPUCACHE}"
echo -e "${RGB_INFO}CPU Basic Frequency    ${RGB_END}: ${CPUMHZ}"
echo -e "${RGB_INFO}Total amount of Memory ${RGB_END}: ${MEMTOTAL} (${MEMFREE} Free)"
echo -e "${RGB_INFO}Total amount of Swap   ${RGB_END}: ${SWAPTOTAL} (${SWAPFREE} Free)"
echo -e "\n${RGB_WARNING}Network Overview (Contains the DNS, IP address and Nameserver)${RGB_END}"
echo -e "${RGB_INFO}IPV4                   ${RGB_END}: ${IPV4}"
if [ ! -z "${IPV6}" ]; then
echo -e "${RGB_INFO}IPV6                   ${RGB_END}: ${IPV6}"
fi
echo -e "${RGB_INFO}Local IP               ${RGB_END}: ${LOCALIP}"
echo -e "${RGB_INFO}MAC Address            ${RGB_END}: ${MACADDRESS}"
echo -e "${RGB_INFO}Nameserver             ${RGB_END}: ${NAMESERVER}"
if [ ! -z "${TENCENTCLOUD}" ]; then
echo -e "\n${RGB_WARNING}Tencent Cloud Overview (Contains the UUID, Instance, Zone and Time)${RGB_END}"
echo -e "${RGB_INFO}UUID                   ${RGB_END}: ${UUID}"
echo -e "${RGB_INFO}Instance ID            ${RGB_END}: ${INSTANCEID}"
echo -e "${RGB_INFO}Instance Name          ${RGB_END}: ${INSTANCENAME}"
echo -e "${RGB_INFO}Region & Zone          ${RGB_END}: ${REGIONZONE}"
echo -e "${RGB_INFO}Charge Type            ${RGB_END}: ${CHARGETYPE}"
echo -e "${RGB_INFO}Create Time            ${RGB_END}: ${CREATETIME}"
echo -e "${RGB_INFO}Termination Time       ${RGB_END}: ${TERMINATIONTIME}"
elif [ ! -z "${ALICLOUD}" ]; then
echo -e "\n${RGB_WARNING}AliCloud Overview (Contains the UUID, Instance and Zone)${RGB_END}"
echo -e "${RGB_INFO}UUID                   ${RGB_END}: ${UUID}"
echo -e "${RGB_INFO}Instance ID            ${RGB_END}: ${INSTANCEID}"
echo -e "${RGB_INFO}Instance Name          ${RGB_END}: ${INSTANCENAME}"
echo -e "${RGB_INFO}Instance Type          ${RGB_END}: ${INSTANCETYPE}"
echo -e "${RGB_INFO}Region & Zone          ${RGB_END}: ${REGIONZONE}"
fi


相关文章
|
2月前
|
NoSQL Linux Redis
在 centos7 下重启/开启 redis 服务器
本文提供了一种在Centos 7操作系统下如何重启Redis服务器的步骤,包括停止Redis服务、确认停止成功以及重新启动Redis服务。
170 2
在 centos7 下重启/开启 redis 服务器
|
2月前
|
运维 Java Linux
【运维基础知识】Linux服务器下手写启停Java程序脚本start.sh stop.sh及详细说明
### 启动Java程序脚本 `start.sh` 此脚本用于启动一个Java程序,设置JVM字符集为GBK,最大堆内存为3000M,并将程序的日志输出到`output.log`文件中,同时在后台运行。 ### 停止Java程序脚本 `stop.sh` 此脚本用于停止指定名称的服务(如`QuoteServer`),通过查找并终止该服务的Java进程,输出操作结果以确认是否成功。
66 1
|
2月前
|
存储 网络协议 Linux
AWS实操-EC2-创建购买linux(centos)EC2服务器
AWS实操-EC2-创建购买linux(centos)EC2服务器
|
2月前
|
弹性计算 关系型数据库 MySQL
CentOS 7.x操作系统的ECS云服务器上搭建WordPress网站
CentOS 7.x操作系统的ECS云服务器上搭建WordPress网站
|
2月前
|
Linux 数据安全/隐私保护 Windows
centos 7.2 搭建svn服务器
centos 7.2 搭建svn服务器
56 0
|
4月前
|
运维 监控 数据库
自动化运维:使用Python脚本实现服务器监控
【8月更文挑战第31天】在这篇文章中,我们将探索如何利用Python编写简单的脚本来实现对服务器的基本监控。通过学习和应用这些技术,你可以快速检测服务器的状态,包括CPU使用率、内存占用和磁盘空间等关键指标。这不仅有助于及时发现问题,还能提升运维效率。文章将逐步引导你理解监控的重要性,并展示如何从零开始构建自己的监控工具。
|
4月前
|
存储 运维 监控
自动化运维:使用Python脚本进行服务器监控
【8月更文挑战第31天】在数字化时代,服务器的稳定运行对于企业至关重要。本文将介绍如何使用Python编写一个简单的服务器监控脚本,帮助运维人员及时发现并解决潜在问题。我们将从基础的服务器资源监控开始,逐步深入到日志分析与报警机制的实现。通过实际代码示例和操作步骤,使读者能够快速掌握自动化监控的技能,提升工作效率。
|
3天前
|
弹性计算 运维 安全
阿里云轻量应用服务器与ECS的区别及选择指南
轻量应用服务器和云服务器ECS(Elastic Compute Service)是两款颇受欢迎的产品。本文将对这两者进行详细的对比,帮助用户更好地理解它们之间的区别,并根据自身需求做出明智的选择。
|
4天前
|
SQL 弹性计算 安全
阿里云上云优选与飞天加速计划活动区别及购买云服务器后续必做功课参考
对于很多用户来说,购买云服务器通常都是通过阿里云当下的各种活动来购买,这就有必要了解这些活动的区别,同时由于活动内的云服务器购买之后还需要单独购买并挂载数据盘,还需要设置远程密码以及安全组等操作之后才能正常使用云服务器。本文就为大家介绍一下目前比较热门的上云优选与飞天加速计划两个活动的区别,以及通过活动来购买云服务器之后的一些必做功课,确保云服务器可以正常使用,以供参考。
|
7天前
|
弹性计算 安全 开发工具
灵码评测-阿里云提供的ECS python3 sdk做安全组管理
批量变更阿里云ECS安全组策略(批量变更)