Last active
August 19, 2022 14:19
-
-
Save mdklapwijk/d8b38836eab13f68d4de88c37b4da6c6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# set-guest-hypervisor-fact v0.1 | |
# | |
# (c)2018 Marcel Klapwijk | |
# Please leave suggestions and bug reports at: | |
# - https://gist.github.com/mdklapwijk/ | |
# | |
# This program is free software; you can redistribute it and/or | |
# modify it under the terms of the GNU General Public License | |
# as published by the Free Software Foundation; either version 2 | |
# of the License, or (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty | |
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# you should have received a copy of the GNU General Public License | |
# along with this program (or with Nagios); if not, write to the | |
# Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
# Boston, MA 02111-1307, USA | |
# | |
# Purpose: | |
# Create a custom puppet fact with details of the hypervisor on | |
# kvm guest systems running gemu-ga. This fact can then be used | |
# by the puppet agent on the guest system to set sysLocation for | |
# snmpd to match the host node in a multi datacenter cluster. | |
# | |
# | |
# Installation: | |
# Place an executable copy of this file in /usr/local/bin on every | |
# kvm hypervisor in your cluster (e.g. Proxmox). | |
# | |
# To make sure the fact on the guest gets updated after a guest | |
# migration to another node in the cluster, place the following line | |
# in a cron like /etc/cron.d/set-guest-hypervisor-fact: | |
# - */5 * * * * root /usr/local/bin/set-guest-hypervisor-fact | |
# | |
# Make sure the puppet installation on the guest system has the | |
# path for custom facts created/available: | |
# - /etc/puppetlabs/facter/facts.d | |
# | |
# | |
# Usage: | |
# If all elements installed and configured correctly (script, cron, | |
# qemu-ga, puppet agent) the facter command should give something | |
# like the following: | |
# root@kvm-guest-01:~/# facter hypervisor | |
# { | |
# fqdn => "hypervisor-node-01.int.domain.com", | |
# hostname => "hypervisor-node-01" | |
# } | |
# | |
# | |
# Note: | |
# NO WARRANTY and NO ERROR_HANDLING, so use at you own risk! | |
# | |
# | |
# Version history: | |
# - 0.1) Initial version. | |
use strict; | |
use warnings; | |
use Net::Domain qw(hostname hostfqdn hostdomain domainname); | |
use MIME::Base64; | |
use IPC::Open2; | |
use JSON; | |
my($file)='/etc/puppetlabs/facter/facts.d/hypervisor.json'; | |
my(%values); | |
my($json)=JSON->new; | |
$values{'hypervisor'}{'hostname'}=hostname(); | |
$values{'hypervisor'}{'fqdn'}=hostfqdn(); | |
my($base64)=encode_base64($json->canonical->pretty->encode(\%values),''); | |
foreach my $qga (glob('/var/run/qemu-server/*.qga')) | |
{ | |
my($handle); | |
my($pid)=open2(*RH, *WH, 'socat -T1 '.$qga.' STDIO'); | |
print WH '{"execute":"guest-file-open", "arguments":{"path":"'.$file.'","mode":"w+"}}'; | |
while(<RH>) | |
{ | |
#print("$_"); | |
if(/{"return": (\d+)}/) | |
{ | |
$handle=$1; | |
} | |
if($handle) | |
{ | |
print WH '{"execute":"guest-file-write", "arguments":{"handle":'.$handle.',"buf-b64":"'.$base64.'"}}'; | |
print WH '{"execute":"guest-file-close", "arguments":{"handle":'.$handle.'}}'; | |
undef($handle); | |
} | |
} | |
close(WH); | |
close(RH); | |
waitpid($pid,0); | |
} | |
exit(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
New qemu-guest-agents does not seem to handle multiline base64 correctly anymore...