banshee est en développement actif : les collecteurs vont encore changer, et une version que vous publiez demandera sans doute des ajustements. Pour un usage stable, attendez la version 1.0.0.

Virtualization

banshee/virtualization-collection/virtualization/vsphere/https/vcenter-7.0u2/

vms

The power state of each virtual machine of a VMware vCenter, against the state expected, by the vCenter REST API.

  • virtualization
  • vmware
  • vsphere
  • vcenter
  • vm
  • https
  • api

par banshee1 installation1 version

v1Dernière

Publiée le 27 octobre 2026. First version.

Keen 1.0.0 · 79 lignes
// The virtual machines of a VMware vCenter, by its REST API (GET /api/vcenter/vm), which vCenter serves under
// /api from vSphere 7.0 U2: the power state of each. The watches of the expected state are not checked by
// default: only the administrator knows which machines must run and which must stay off, and says it by the
// selection of the probe, one probe for each.
// The address of the API is the vCenter's, the host's: https://<host:address>/api/.
need('protocol:http');
$address = need('host:address');
$user = need('protocol:http:user');
$password = need('protocol:http:password');

per('vm', 'Virtual machines');
output('power', 'text', array('label' => 'Power', 'per' => 'vm'));

watch.expect('not-on', 'Virtual machine not powered on', 'power', array(
    'help'     => 'For a selection of the machines that must run: critical when one is off or suspended.',
    'critical' => array('powered-on'),
    'enabled'  => false,
));
watch.expect('not-off', 'Virtual machine not powered off', 'power', array(
    'help'    => 'For a selection of the machines that must stay off: warning when one runs.',
    'warning' => array('powered-off'),
    'enabled' => false,
));
watch.missing('missing', 'Virtual machine missing', 'power', array('enabled' => false));

// Log in: POST /api/session, the account in Basic, answers 201 and the id of the session as a JSON text. The
// answer is not secret by itself, the password having gone in Basic, but the id is: secret.mark keeps it out
// of every value and message, and the cache keeps it encrypted on the poller, 50 minutes at most.
function login($address, $user, $password) {
    $answer = http.post('https://' . $address . '/api/session', '', user=$user, password=$password);
    if ($answer['status'] == 401 || $answer['status'] == 403) {
        fail('misconfigured', 'vCenter refuses the account, code ' . $answer['status']);
    }
    if ($answer['status'] != 201) {
        fail('unreachable', 'vCenter answers the login with code ' . $answer['status'],
            'vCenter serves its REST API under /api from vSphere 7.0 U2.');
    }
    $session = secret.mark(json.read($answer['body']));
    cache.set('session', $session, 3000);
    return $session;
}

// One GET with the session kept in the cache; a session vCenter no longer knows (expired, vCenter restarted)
// answers 401, and the collector logs in again, once. vCenter answers 500 when more than 4,000 machines
// match: it lists none rather than a part.
if (cache.has('session')) {
    $session = cache.get('session');
} else {
    $session = login($address, $user, $password);
}
$answer = http.get('https://' . $address . '/api/vcenter/vm', headers=array('vmware-api-session-id' => $session));
if ($answer['status'] == 401) {
    $session = login($address, $user, $password);
    $answer = http.get('https://' . $address . '/api/vcenter/vm', headers=array('vmware-api-session-id' => $session));
}
if ($answer['status'] == 403) {
    fail('misconfigured', 'The account may not read the virtual machines, code 403');
}
if ($answer['status'] == 500) {
    fail('unreachable', 'vCenter answers the list of the virtual machines with code 500',
        'vCenter refuses to list more than 4,000 virtual machines at once.');
}
if ($answer['status'] != 200) {
    fail('unreachable', 'vCenter answers the list of the virtual machines with code ' . $answer['status']);
}

// [{"vm": "vm-42", "name": "web01", "power_state": "POWERED_ON", "cpu_count": 2, "memory_size_MiB": 4096}].
// The power is returned in lower case, "_" written "-". Two machines of one name (in two folders) each keep
// their own, the second carrying its id.
$vms = json.read($answer['body']);
$seen = array();
foreach ($vms as $vm) {
    $name = $vm['name'];
    if (list.has($seen, $name)) {
        $name = $name . ' #' . $vm['vm'];
    }
    $seen[$name] = true;
    value('power', text.join(text.split(text.lower($vm['power_state']), '_'), '-'), $name);
}