#!/usr/bin/python3
#
# Check every 60 seconds to see if nobody is logged in and no meetings
# are running, and if not, hibernate a list of AWS EC2 instances.

import time
import boto3
import psutil
import datetime

import bigbluebutton

AWS_REGION = 'us-east-2'
REMOTE_INSTANCE_LIST_STOP = ["i-046861ce5d597be6b", "i-0e997d8963457c7fa"]
REMOTE_INSTANCE_LIST_HIBERNATE = ["i-08f39feb40bc52bd7"]

ec2_handle = None

def ec2():
    global ec2_handle
    if ec2_handle == None:
        session = boto3.Session(region_name = AWS_REGION)
        ec2_handle = session.client('ec2')
    return ec2_handle

def is_server_idle():
    try:
        if len(psutil.users()) > 0:
            return False
        xml = bigbluebutton.getMeetings()
        return len(xml.xpath('.//meeting')) == 0
    except:
        return False

def main():
    while True:
        if is_server_idle():
            print("Hibernating instances at", datetime.datetime.now())
            ec2().stop_instances(InstanceIds=REMOTE_INSTANCE_LIST_STOP)
            ec2().stop_instances(InstanceIds=REMOTE_INSTANCE_LIST_HIBERNATE, Hibernate=True)
        time.sleep(60)

if __name__ == "__main__":
    main()

6b6474fc8ffb7d64fff1010d2b959b5def59d592
<mask token>


def ec2():
    global ec2_handle
    if ec2_handle == None:
        session = boto3.Session(region_name=AWS_REGION)
        ec2_handle = session.client('ec2')
    return ec2_handle


def is_server_idle():
    try:
        if len(psutil.users()) > 0:
            return False
        xml = bigbluebutton.getMeetings()
        return len(xml.xpath('.//meeting')) == 0
    except:
        return False


def main():
    while True:
        if is_server_idle():
            print('Hibernating instances at', datetime.datetime.now())
            ec2().stop_instances(InstanceIds=REMOTE_INSTANCE_LIST_STOP)
            ec2().stop_instances(InstanceIds=REMOTE_INSTANCE_LIST_HIBERNATE,
                Hibernate=True)
        time.sleep(60)


<mask token>

<mask token>


def ec2():
    global ec2_handle
    if ec2_handle == None:
        session = boto3.Session(region_name=AWS_REGION)
        ec2_handle = session.client('ec2')
    return ec2_handle


def is_server_idle():
    try:
        if len(psutil.users()) > 0:
            return False
        xml = bigbluebutton.getMeetings()
        return len(xml.xpath('.//meeting')) == 0
    except:
        return False


def main():
    while True:
        if is_server_idle():
            print('Hibernating instances at', datetime.datetime.now())
            ec2().stop_instances(InstanceIds=REMOTE_INSTANCE_LIST_STOP)
            ec2().stop_instances(InstanceIds=REMOTE_INSTANCE_LIST_HIBERNATE,
                Hibernate=True)
        time.sleep(60)


if __name__ == '__main__':
    main()

<mask token>
AWS_REGION = 'us-east-2'
REMOTE_INSTANCE_LIST_STOP = ['i-046861ce5d597be6b', 'i-0e997d8963457c7fa']
REMOTE_INSTANCE_LIST_HIBERNATE = ['i-08f39feb40bc52bd7']
ec2_handle = None


def ec2():
    global ec2_handle
    if ec2_handle == None:
        session = boto3.Session(region_name=AWS_REGION)
        ec2_handle = session.client('ec2')
    return ec2_handle


def is_server_idle():
    try:
        if len(psutil.users()) > 0:
            return False
        xml = bigbluebutton.getMeetings()
        return len(xml.xpath('.//meeting')) == 0
    except:
        return False


def main():
    while True:
        if is_server_idle():
            print('Hibernating instances at', datetime.datetime.now())
            ec2().stop_instances(InstanceIds=REMOTE_INSTANCE_LIST_STOP)
            ec2().stop_instances(InstanceIds=REMOTE_INSTANCE_LIST_HIBERNATE,
                Hibernate=True)
        time.sleep(60)


if __name__ == '__main__':
    main()

import time
import boto3
import psutil
import datetime
import bigbluebutton
AWS_REGION = 'us-east-2'
REMOTE_INSTANCE_LIST_STOP = ['i-046861ce5d597be6b', 'i-0e997d8963457c7fa']
REMOTE_INSTANCE_LIST_HIBERNATE = ['i-08f39feb40bc52bd7']
ec2_handle = None


def ec2():
    global ec2_handle
    if ec2_handle == None:
        session = boto3.Session(region_name=AWS_REGION)
        ec2_handle = session.client('ec2')
    return ec2_handle


def is_server_idle():
    try:
        if len(psutil.users()) > 0:
            return False
        xml = bigbluebutton.getMeetings()
        return len(xml.xpath('.//meeting')) == 0
    except:
        return False


def main():
    while True:
        if is_server_idle():
            print('Hibernating instances at', datetime.datetime.now())
            ec2().stop_instances(InstanceIds=REMOTE_INSTANCE_LIST_STOP)
            ec2().stop_instances(InstanceIds=REMOTE_INSTANCE_LIST_HIBERNATE,
                Hibernate=True)
        time.sleep(60)


if __name__ == '__main__':
    main()

#!/usr/bin/python3
#
# Check every 60 seconds to see if nobody is logged in and no meetings
# are running, and if not, hibernate a list of AWS EC2 instances.

import time
import boto3
import psutil
import datetime

import bigbluebutton

AWS_REGION = 'us-east-2'
REMOTE_INSTANCE_LIST_STOP = ["i-046861ce5d597be6b", "i-0e997d8963457c7fa"]
REMOTE_INSTANCE_LIST_HIBERNATE = ["i-08f39feb40bc52bd7"]

ec2_handle = None

def ec2():
    global ec2_handle
    if ec2_handle == None:
        session = boto3.Session(region_name = AWS_REGION)
        ec2_handle = session.client('ec2')
    return ec2_handle

def is_server_idle():
    try:
        if len(psutil.users()) > 0:
            return False
        xml = bigbluebutton.getMeetings()
        return len(xml.xpath('.//meeting')) == 0
    except:
        return False

def main():
    while True:
        if is_server_idle():
            print("Hibernating instances at", datetime.datetime.now())
            ec2().stop_instances(InstanceIds=REMOTE_INSTANCE_LIST_STOP)
            ec2().stop_instances(InstanceIds=REMOTE_INSTANCE_LIST_HIBERNATE, Hibernate=True)
        time.sleep(60)

if __name__ == "__main__":
    main()
