#!/usr/bin/env pmpython
#
# Copyright (C) 2016-2024 Marko Myllynen <myllynen@redhat.com>
#
# 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.
#

try:
    import ConfigParser
except ImportError:
    import configparser as ConfigParser

import os
import pwd
import sys
import libvirt

from pcp.pmapi import pmContext as PCP

conffile = PCP.pmGetConfig('PCP_PMDAS_DIR')
conffile += '/libvirt/libvirt.conf'

user = 'root'
uri = 'qemu:///system'
backing = False

# Python < 3.2 compat
if sys.version_info[0] >= 3 and sys.version_info[1] >= 2:
    config = ConfigParser.ConfigParser()
else:
    config = ConfigParser.SafeConfigParser()
config.read(conffile)
if config.has_section('pmda'):
    for opt in config.options('pmda'):
        if opt == 'user':
            user = config.get('pmda', opt)
        elif opt == 'uri':
            uri = config.get('pmda', opt)
        elif opt == 'backing':
            if config.get('pmda', opt) == 'True' or \
            config.get('pmda', opt) == '1':
                backing = True
        elif opt == 'oldapi':
            sys.stdout.write("Ignoring obsolete directive '%s' in %s." % (opt, conffile))
        else:
            sys.stderr.write("Invalid directive '%s' in %s.\n" % (opt, conffile))
            sys.exit(1)

if len(sys.argv) > 1 and (sys.argv[1] == '-c' or sys.argv[1] == '--config'):
    sys.stdout.write("user=%s\nuri=%s\nbacking=%s\n" % (user, uri, backing))
    sys.exit(0)

try:
    uid = pwd.getpwnam(user).pw_uid
    os.setuid(uid)
except:
    sys.stderr.write("Failed to switch as user %s, try sudo perhaps?\n" % user)
    sys.exit(1)

try:
    conn = libvirt.openReadOnly(uri)
    doms = conn.listAllDomains(libvirt.VIR_CONNECT_LIST_DOMAINS_ACTIVE)
except Exception as e:
    sys.stdout.write("Connection as %s to %s failed: %s\n" % (user, uri, e))
    sys.exit(1)

sys.stdout.write("Connection as %s to %s ok.\n" % (user, uri))

sys.stdout.write("Block dev backing stats: %s.\n" % backing)
