前言
企业微信机器人自动化获取PVE系统信息脚本编写
- 生成FIGlet和AOL宏字体 参考
- 最终实现获取系统信息,获取到的信息发送给企业微信机器人进行输出
- 企业微信机器人创建参考 更改以下脚本企业微信机器人地址
- 企业微信机器人url工具往群组推送文本消息参考配置
- WECHAT_ROBOT_URL=自己插件的企业微信地址
vim /proxmox_Enterprise_wechat_notification.sh
#!/bin/bash
# -*- coding: utf-8 -*-
# Author: make.han
# Email: CIASM@CIASM
# date 2024-04-23
# Enterprise wechat robot notification module
#echo "installing plugins"
#apt install jq qemu-guest-agent -y
function Deployment_completion_notification (){
# Enterprise wechat robot address
WECHAT_ROBOT_URL='https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=YOU_key'
# Obtain system, software, hardware information
host_ID=`dmidecode -s system-serial-number | sed -r 's/\s+//g'`
host_IP=`ip addr show vmbr0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'`
# Proxmox VE Memory display
memory_Size=`dmidecode -t memory | grep Size | grep -v No | awk '{sum+=$2} END {printf "%.0fG\n",sum/1^C4}'`
CPU_Model=`cat /proc/cpuinfo | grep 'model name' | awk '{print $6}' | uniq`
PowerEdge=`dmidecode | grep "Product Name: P" | awk '{print $4}'`
Proxmox_VE_version=`pveversion | awk '{print $1}' | awk -F'/' '{print $2}'`
Proxmox_VE_core=`cat /proc/version | grep "version" | awk '{print $3}'`
# check Proxmox_VE all Network
#Proxmox_VE_all_ip=`ip addr | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | grep -v '127.0.0.1' | paste -s -d '-'`
Proxmox_VE_all_ip=`ip addr | awk '/inet / && !/127.0.0.1/ {split($2, a, "/"); printf "%s-(%s)/", $NF, a[1]} END {printf "\n"}' | sed 's/-,$//'`
# Proxmox VE check network name
NIC_NAME_0=`ip link show | awk -F': ' '/^[6]+:/{print $2; exit}'`
NIC_NAME_1=`ip link show | awk -F': ' '/^[7]+:/{print $2; exit}'`
NIC_NAME_2=`ip link show | awk -F': ' '/^[2]+:/{print $2; exit}'`
NIC_NAME_3=`ip link show | awk -F': ' '/^[3]+:/{print $2; exit}'`
NIC_NAME_4=`ip link show | awk -F': ' '/^[4]+:/{print $2; exit}'`
NIC_NAME_5=`ip link show | awk -F': ' '/^[5]+:/{print $2; exit}'`
# The VM information is displayed
#vmid=`qm list | awk 'NR>1 {print $1}' | paste -s -d ','`
vmid=`qm list | awk 'NR>1 {printf "%svmid(%s)", (NR==2 ? "" : "/"), $1, $1} END {print ""}'`
#vm_name=`qm list | awk 'NR>1 {print $2}' | paste -s -d ','`
vm_name=`qm list | awk 'NR>1 {printf "%svmid%s-(%s)", (NR==2 ? "" : "/"), $1, $2} END {print ""}'`
#vm_memory_MB=`qm list | awk 'NR>1 {print $4}' | paste -s -d ','`
vm_memory_MB=`qm list | awk 'NR>1 {printf "%svmid%s-(%sMB)", (NR==2 ? "" : "/"), $1, $4} END {print ""}'`
# 只获取虚拟机磁盘容量
#vm_bootdisk_GB=`qm list | awk 'NR>1 {print $5}' | paste -s -d ','`
# Obtain the VM disk and the id of the VM
vm_bootdisk_GB=`qm list | awk 'NR>1 {printf "%svmid%s-(%sGB)", (NR==2 ? "" : "/"), $1, $5} END {print ""}'`
#vm_status=`qm list | awk 'NR>1 {print $3}' | paste -s -d ','`
vm_status=`qm list | awk 'NR>1 {printf "%svmid%s-(%s)", (NR==2 ? "" : "/"), $1, $3} END {print ""}'`
# Obtain only the IP address of the VM
#vm_host_IP=`qm list | awk 'NR>1 {print $1}' | xargs -I {} sh -c 'qm guest cmd {} network-get-interfaces | jq -r ".[] | select(.\"ip-addresses\" != null) | .\"ip-addresses\"[] | select(.\"ip-address-type\" == \"ipv4\" and .\"ip-address\" != \"127.0.0.1\") | .\"ip-address\"" | tr "\n" ","' | awk '{print substr($0, 1, length-1)}'`
# Obtain the VM IP address and VM id
vm_host_IP=`qm list | awk 'NR>1 {print $1}' | xargs -I {} sh -c 'printf "vmid{}-"; qm guest cmd {} network-get-interfaces | jq -r ".[] | select(.\"ip-addresses\" != null) | .\"ip-addresses\"[] | select(.\"ip-address-type\" == \"ipv4\" and .\"ip-address\" != \"127.0.0.1\") | .\"ip-address\"" | tr "\n" "," | sed "s/,$//" && echo ""' | awk '{$1=$1;print}' | tr "\n" "/ " | sed 's/,$//'`
#vm_host_IP=`qm list | awk 'NR>1 {print $1}' | xargs -I {} sh -c 'printf "vmid{}-"; qm guest cmd {} network-get-interfaces | jq -r ".[] | select(.\"ip-addresses\" != null) | .\"ip-addresses\"[] | select(.\"ip-address-type\" == \"ipv4\" and .\"ip-address\" != \"127.0.0.1\") | .\"ip-address\"" | tr "\n" "," | sed "s/,$//" && echo ""' | awk '{$1=$1;print}' | tr "\n" ", " | sed 's/,$//'`
curl $WECHAT_ROBOT_URL \
-H 'Content-Type: application/json' \
-d '{
"msgtype": "markdown",
"markdown": {
"content": "**Proxmox VE System** <font color=\"info\">complete</font>\n
> **PVE Management address** \n
[https://'$host_IP':8006](https://'$host_IP':8006)\n
> **Hardware information** \n
hostSN:<font color=\"info\">'$host_ID'</font>\n
CPU_Model:<font color=\"info\">'$CPU_Model'</font>\n
memory_Size:<font color=\"info\">'$memory_Size'</font>\n
PowerEdge:<font color=\"info\">'$PowerEdge'</font>\n
Proxmox_VE_version:<font color=\"info\">'$Proxmox_VE_version'</font>\n
Proxmox_VE_core:<font color=\"info\">'$Proxmox_VE_core'</font>\n
NIC_0:<font color=\"info\">'$NIC_NAME_0'</font>\n
NIC_1:<font color=\"info\">'$NIC_NAME_1'</font>\n
NIC_2:<font color=\"info\">'$NIC_NAME_2'</font>\n
NIC_3:<font color=\"info\">'$NIC_NAME_3'</font>\n
NIC_4:<font color=\"info\">'$NIC_NAME_4'</font>\n
NIC_5:<font color=\"info\">'$NIC_NAME_5'</font>\n
Proxmox_VE_all_ip:<font color=\"info\">'$Proxmox_VE_all_ip'</font>\n
> **Current virtual machine** \n
VM_ID:<font color=\"info\">'$vmid'</font>\n
VM_Name:<font color=\"info\">'$vm_name'</font>\n
VM_memory_GB:<font color=\"info\">'$vm_memory_MB'</font>\n
VM_bootdisk_GB:<font color=\"info\">'$vm_bootdisk_GB'</font>\n
VM_status:<font color=\"info\">'$vm_status'</font>\n
vm_host_IP:<font color=\"info\">'$vm_host_IP'</font>\n"
}
}'
}
function main (){
Deployment_completion_notification
}
main
执行脚本
bash /proxmox_Enterprise_wechat_notification.sh
企业微信通知