<?php
/**
 * Checks current directory for a running Vagrant virtual machine
 *
 * Author: John Kary <john@johnkary.net>
 */
$cwd = getcwd();
$filecontents = getVagrantFileRunningInDir($cwd);

if (false === $filecontents) {
    // No vagrant file in this dir
    exit(1);
}

$uuid = getDirVagrantUuid($filecontents);
$runningVms = getRunningVms();

if (!isVmRunning($runningVms, $uuid)) {
    // VM not running
    exit(1);
}

// VM running
exit(0);


function getVagrantFileRunningInDir($dir)
{
    $path = realpath($dir . '/.vagrant');

    if (false === $path) {
        // File with UUID doesn't exist
        return false;
    }

    return file_get_contents($path);
}
function getDirVagrantUuid($filecontents)
{
    $vagrantJson = json_decode($filecontents);

    return $vagrantJson->active->default;
}
function getRunningVms()
{
    exec('vboxmanage list runningvms', $output);

    return $output;
}
function isVmRunning($runningVms, $uuid)
{
    return (bool) strstr(implode(' ', $runningVms), $uuid);
}

<?php
/**
 * Checks current directory for a running Vagrant virtual machine
 *
 * Author: John Kary <john@johnkary.net>
 */
$cwd = getcwd();
$uuid = getVagrantUuidInDir($cwd);

if (false === $uuid) {
    // No vagrant file in this dir
    exit(1);
}

$runningVms = getRunningVms();
if (!isVmRunning($runningVms, $uuid)) {
    // VM not running
    exit(1);
}

// VM running
exit(0);


function getVagrantUuidInDir($dir)
{
    $path = realpath($dir . '/.vagrant/machines/default/virtualbox/id');

    if (false === $path) {
        // File with UUID doesn't exist
        return false;
    }

    return file_get_contents($path);
}
function getRunningVms()
{
    exec('vboxmanage list runningvms', $output);

    return $output;
}
function isVmRunning($runningVms, $uuid)
{
    return (bool) strstr(implode(' ', $runningVms), $uuid);
}

Update for compatibility with Vagrant 1.2
Update for compatibility with Vagrant 1.2

1.2 changed how the box UUID was written to the filesystem. Instead of
JSON it's now written as a single file .vagrant/machines/default/virtualbox/id
