initial commit
This commit is contained in:
58
pve-netbox-diff.sh
Normal file
58
pve-netbox-diff.sh
Normal file
@@ -0,0 +1,58 @@
|
||||
#! /bin/bash
|
||||
|
||||
#set -e
|
||||
|
||||
NETBOX_URL=""
|
||||
NETBOX_AUTH_TOKEN=""
|
||||
MAILTO=""
|
||||
SUBJECT="PVE and NetBox mismatch"
|
||||
|
||||
curl -s -X GET "$NETBOX_URL/api/virtualization/virtual-machines/" \
|
||||
-H "Authorization: Token $NETBOX_AUTH_TOKEN" \
|
||||
-H 'Accept: application/json' | \
|
||||
jq -r '
|
||||
[
|
||||
.results[] |
|
||||
{
|
||||
VMID: .custom_fields.VMID,
|
||||
status: (.status.value |
|
||||
gsub("active"; "running") |
|
||||
gsub("offline"; "stopped")),
|
||||
name: .name
|
||||
}
|
||||
] |
|
||||
sort_by(.VMID) |
|
||||
map([.VMID, .status, .name]) |
|
||||
(["VMID", "Status", "Name"]) as $header |
|
||||
$header, .[] |
|
||||
@csv' | \
|
||||
tr -d '"' > /tmp/netbox-status.csv
|
||||
|
||||
/usr/sbin/pct list | awk 'NR==1 {print "VMID,Status,Name"} NR>1 {print $1","$2","$3}' > /tmp/pve-status.csv
|
||||
/usr/sbin/qm list | awk 'NR>1 {print $1","$3","$2}' OFS="," >> /tmp/pve-status.csv
|
||||
|
||||
diff -C 0 /tmp/pve-status.csv /tmp/netbox-status.csv > /tmp/diff_result.txt
|
||||
EXIT_STATUS=$?
|
||||
|
||||
if [ $EXIT_STATUS -eq 1 ]; then
|
||||
for host in $(diff -C 0 /tmp/pve-status.csv /tmp/netbox-status.csv | grep '^- ' | awk -F ',' '{sub(/^- /, ""); print $1; }'); do
|
||||
if [[ ${host:0:1} -eq 1 ]]; then
|
||||
echo "Resources for $host:" >> /tmp/new-host-resources.txt
|
||||
/usr/sbin/pct config $host | grep -E '^hostname|^cores|^memory|^rootfs|^net' >> /tmp/new-host-resources.txt
|
||||
echo >> /tmp/new-host-resources.txt
|
||||
elif [[ ${host:0:1} -eq 2 ]]; then
|
||||
echo "Resources for $host:" >> /tmp/new-host-resources.txt
|
||||
/usr/sbin/qm config $host | grep -E '^name|^cores|^memory|^scsi[0-9]|^net' >> /tmp/new-host-resources.txt
|
||||
echo >> /tmp/new-host-resources.txt
|
||||
fi
|
||||
done
|
||||
|
||||
if [ -f /tmp/new-host-resources.txt ]; then
|
||||
awk 'FNR==1 && NR!=1 {print ""; print "Here is a list of hosts missing from NetBox together with their information:"}; {print}' /tmp/diff_result.txt /tmp/new-host-resources.txt > /tmp/mail.txt
|
||||
else
|
||||
cp /tmp/diff_result.txt /tmp/mail.txt
|
||||
fi
|
||||
mail -s "$SUBJECT" "$MAILTO" < /tmp/mail.txt
|
||||
fi
|
||||
|
||||
rm -f /tmp/pve-status.csv /tmp/netbox-status.csv /tmp/diff_result.txt /tmp/new-host-resources.txt /tmp/mail.txt
|
||||
Reference in New Issue
Block a user