【小技巧分享】通过脚本快速获取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


相关文章
|
1月前
|
NoSQL Linux Redis
在 centos7 下重启/开启 redis 服务器
本文提供了一种在Centos 7操作系统下如何重启Redis服务器的步骤,包括停止Redis服务、确认停止成功以及重新启动Redis服务。
97 2
在 centos7 下重启/开启 redis 服务器
|
1月前
|
运维 Java Linux
【运维基础知识】Linux服务器下手写启停Java程序脚本start.sh stop.sh及详细说明
### 启动Java程序脚本 `start.sh` 此脚本用于启动一个Java程序,设置JVM字符集为GBK,最大堆内存为3000M,并将程序的日志输出到`output.log`文件中,同时在后台运行。 ### 停止Java程序脚本 `stop.sh` 此脚本用于停止指定名称的服务(如`QuoteServer`),通过查找并终止该服务的Java进程,输出操作结果以确认是否成功。
34 1
|
28天前
|
存储 网络协议 Linux
AWS实操-EC2-创建购买linux(centos)EC2服务器
AWS实操-EC2-创建购买linux(centos)EC2服务器
|
29天前
|
弹性计算 关系型数据库 MySQL
CentOS 7.x操作系统的ECS云服务器上搭建WordPress网站
CentOS 7.x操作系统的ECS云服务器上搭建WordPress网站
|
1月前
|
Linux 数据安全/隐私保护 Windows
centos 7.2 搭建svn服务器
centos 7.2 搭建svn服务器
50 0
|
3月前
|
运维 监控 数据库
自动化运维:使用Python脚本实现服务器监控
【8月更文挑战第31天】在这篇文章中,我们将探索如何利用Python编写简单的脚本来实现对服务器的基本监控。通过学习和应用这些技术,你可以快速检测服务器的状态,包括CPU使用率、内存占用和磁盘空间等关键指标。这不仅有助于及时发现问题,还能提升运维效率。文章将逐步引导你理解监控的重要性,并展示如何从零开始构建自己的监控工具。
|
3月前
|
存储 运维 监控
自动化运维:使用Python脚本进行服务器监控
【8月更文挑战第31天】在数字化时代,服务器的稳定运行对于企业至关重要。本文将介绍如何使用Python编写一个简单的服务器监控脚本,帮助运维人员及时发现并解决潜在问题。我们将从基础的服务器资源监控开始,逐步深入到日志分析与报警机制的实现。通过实际代码示例和操作步骤,使读者能够快速掌握自动化监控的技能,提升工作效率。
|
4天前
|
机器学习/深度学习 人工智能 弹性计算
什么是阿里云GPU云服务器?GPU服务器优势、使用和租赁费用整理
阿里云GPU云服务器提供强大的GPU算力,适用于深度学习、科学计算、图形可视化和视频处理等多种场景。作为亚太领先的云服务提供商,阿里云的GPU云服务器具备灵活的资源配置、高安全性和易用性,支持多种计费模式,帮助企业高效应对计算密集型任务。
|
6天前
|
存储 分布式计算 固态存储
阿里云2核16G、4核32G、8核64G配置云服务器租用收费标准与活动价格参考
2核16G、8核64G、4核32G配置的云服务器处理器与内存比为1:8,这种配比的云服务器一般适用于数据分析与挖掘,Hadoop、Spark集群和数据库,缓存等内存密集型场景,因此,多为企业级用户选择。目前2核16G配置按量收费最低收费标准为0.54元/小时,按月租用标准收费标准为260.44元/1个月。4核32G配置的阿里云服务器按量收费标准最低为1.08元/小时,按月租用标准收费标准为520.88元/1个月。8核64G配置的阿里云服务器按量收费标准最低为2.17元/小时,按月租用标准收费标准为1041.77元/1个月。本文介绍这些配置的最新租用收费标准与活动价格情况,以供参考。
|
4天前
|
机器学习/深度学习 人工智能 弹性计算
阿里云GPU服务器全解析_GPU价格收费标准_GPU优势和使用说明
阿里云GPU云服务器提供强大的GPU算力,适用于深度学习、科学计算、图形可视化和视频处理等场景。作为亚太领先的云服务商,阿里云GPU云服务器具备高灵活性、易用性、容灾备份、安全性和成本效益,支持多种实例规格,满足不同业务需求。