3 # Copyright (C) 2016 Marko Myllynen <myllynen@redhat.com>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
19 import configparser
as ConfigParser
26 from pcp
.pmapi
import pmContext
as PCP
28 conffile
= PCP
.pmGetConfig('PCP_PMDAS_DIR')
29 conffile
+= '/libvirt/libvirt.conf'
32 uri
= 'qemu:///system'
34 config
= ConfigParser
.SafeConfigParser()
36 if config
.has_section('pmda'):
37 for opt
in config
.options('pmda'):
39 user
= config
.get('pmda', opt
)
41 uri
= config
.get('pmda', opt
)
43 sys
.stderr
.write("Invalid directive '%s' in %s.\n" % (opt
, conffile
))
46 if len(sys
.argv
) > 1 and (sys
.argv
[1] == '-c' or sys
.argv
[1] == '--config'):
47 sys
.stdout
.write("user=%s\nuri=%s\n" % (user
, uri
))
51 uid
= pwd
.getpwnam(user
).pw_uid
54 sys
.stderr
.write("Failed to switch as user %s, try sudo perhaps?\n" % user
)
58 conn
= libvirt
.openReadOnly(uri
)
59 doms
= conn
.listAllDomains(libvirt
.VIR_CONNECT_LIST_DOMAINS_ACTIVE
)
61 sys
.stdout
.write("Connection as %s to %s failed!\n" % (user
, uri
))
64 sys
.stdout
.write("Connection as %s to %s ok.\n" % (user
, uri
))