Example #1
0
def get_env_info():
    '''Gets general information about the computer.'''
    
    infodict = OrderedDict()
    
    infodict["Name"] = platform.node()
    infodict["System"] = platform.system()
    infodict["System alias"] = " ".join(platform.system_alias(
            platform.system(), platform.release(), platform.version()))
    infodict["Platform"] = platform.platform()
    
    if infodict["System"] == "Linux": # System-specific information
        infodict["Distribution"] = " ".join(platform.dist())
    elif infodict["System"] == "Windows":
        infodict["OS"] = " ".join(platform.win32_ver())
    elif infodict["System"] == "MacOS":
        verinfo = platform.mac_ver()[1]
        macver = " ".join(platform.mac_ver())
        macver[1] = verinfo
        infodict["OS"] = " ".join(macver)
    
    infodict["Boot time"] = datetime.datetime.fromtimestamp(
            psutil.get_boot_time()).strftime("%c")
    infodict["Uptime"] = str(datetime.datetime.fromtimestamp(
            time.time() - psutil.get_boot_time()).strftime("%d:%H:%M:%S:%f"))
    
    for user in psutil.get_users():
        infodict["User '" + user.name + "' terminal"] = user.terminal
        infodict["User '" + user.name + "' host"] = user.host
        infodict["User '" + user.name + "' started"] = str(
                datetime.datetime.fromtimestamp(user.started).strftime("%c"))
    
    return infodict
Example #2
0
def boot_time(time_format=None):
    '''
    Return the boot time in number of seconds since the epoch began.

    CLI Example:

    time_format
        Optionally specify a `strftime`_ format string. Use
        ``time_format='%c'`` to get a nicely-formatted locale specific date and
        time (i.e. ``Fri May  2 19:08:32 2014``).

        .. _strftime: https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior

        .. versionadded:: 2014.1.4

    .. code-block:: bash

        salt '*' ps.boot_time
    '''
    try:
        b_time = int(psutil.boot_time())
    except AttributeError:
        # get_boot_time() has been removed in newer psutil versions, and has
        # been replaced by boot_time() which provides the same information.
        b_time = int(psutil.get_boot_time())
    if time_format:
        # Load epoch timestamp as a datetime.datetime object
        b_time = datetime.datetime.fromtimestamp(b_time)
        try:
            return b_time.strftime(time_format)
        except TypeError as exc:
            raise SaltInvocationError('Invalid format string: {0}'.format(exc))
    return b_time
Example #3
0
def stats():
    # OS identification
    yield 'osname', osname()

    # Uptime
    yield 'uptime', time.time() - psutil.get_boot_time()

    # CPU temperature
    try:
        temps = []

        for line in check_output('sensors').split('\n'):
            m = re.match(r'^Core \d+:\s*\+(\d+\.\d+)', line)

            if m:
                temps.append(float(m.group(1)))

        assert len(temps) == psutil.NUM_CPUS
        yield 'temps', temps
    except:
        pass

    # CPU usage
    yield 'cpu_usage', round(psutil.cpu_percent(), 2)

    # Memory usage
    mem = psutil.phymem_usage()
    yield 'memory', (mem.used, mem.total)

    # Disk usage
    disk = psutil.disk_usage('/')
    yield 'disk', (disk.used, disk.total)
Example #4
0
def stats():
    # OS identification
    yield 'osname', osname()

    # Uptime
    yield 'uptime', time.time() - psutil.get_boot_time()

    # CPU temperature
    try:
        temps = []

        for line in check_output('sensors').split('\n'):
            m = re.match(r'^Core \d+:\s*\+(\d+\.\d+)', line)

            if m:
                temps.append(float(m.group(1)))

        assert len(temps) == psutil.NUM_CPUS
        yield 'temps', temps
    except:
        pass

    # CPU usage
    yield 'cpu_usage', round(psutil.cpu_percent(), 2)

    # Memory usage
    mem = psutil.phymem_usage()
    yield 'memory', (mem.used, mem.total)

    # Disk usage
    disk = psutil.disk_usage('/')
    yield 'disk', (disk.used, disk.total)
Example #5
0
    def _basic_monitoring(self):

        status = {}

        memory = psutil.virtual_memory()

        total_memory_mb = int(memory.total) / 1024
        free_memory_mb = int(memory.free) / 1024
        used_memory_mb = total_memory_mb - free_memory_mb

        memory_utilization = round(
            float(used_memory_mb) / float(total_memory_mb) * 100, 2)

        disk = psutil.disk_usage("/")

        status["agent_id"] = self.agent_context.agent_id
        status["timestamp"] = int(time.time())

        status["total_disk_GB"] = disk[0]
        status["disk_utilization"] = disk[3]

        if int(psutil.__version__.split(".")[0]) > 2:
            status["cpu_cores"] = int(psutil.cpu_count())
            status["boot_time"] = int(psutil.boot_time())
        else:
            status["cpu_cores"] = int(psutil.NUM_CPUS)
            status["boot_time"] = int(psutil.get_boot_time())

        status["cpu_utilization"] = psutil.cpu_percent(interval=1)
        status["total_memory_MB"] = total_memory_mb
        status["memory_utilization"] = memory_utilization

        logger.debug("Basic monitoring: %s" % (json.dumps(status)))

        return status
Example #6
0
def index():
    load_avg = os.getloadavg()
    uptime = datetime.now() - datetime.fromtimestamp(psutil.get_boot_time())
    disks = get_disks()
    users = get_users()

    netifs = get_network_interfaces()
    netifs.sort(key=lambda x: x.get("bytes_sent"), reverse=True)

    data = {
        "os": platform.platform().decode("utf-8"),
        "hostname": socket.gethostname().decode("utf-8"),
        "uptime": str(uptime).split(".")[0],
        "load_avg": load_avg,
        "cpus": psutil.NUM_CPUS,
        "vmem": psutil.virtual_memory(),
        "swap": psutil.swap_memory(),
        "disks": disks,
        "cpu_percent": psutil.cpu_times_percent(0),
        "users": users,
        "net_interfaces": netifs,
        "page": "overview",
        "is_xhr": request.is_xhr
    }

    return render_template("index.html", **data)
Example #7
0
File: ps.py Project: zsjohny/salt
def boot_time(time_format=None):
    '''
    Return the boot time in number of seconds since the epoch began.

    CLI Example:

    time_format
        Optionally specify a `strftime`_ format string. Use
        ``time_format='%c'`` to get a nicely-formatted locale specific date and
        time (i.e. ``Fri May  2 19:08:32 2014``).

        .. _strftime: https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior

        .. versionadded:: 2014.1.4

    .. code-block:: bash

        salt '*' ps.boot_time
    '''
    try:
        b_time = int(psutil.boot_time())
    except AttributeError:
        # get_boot_time() has been removed in newer psutil versions, and has
        # been replaced by boot_time() which provides the same information.
        b_time = int(psutil.get_boot_time())
    if time_format:
        # Load epoch timestamp as a datetime.datetime object
        b_time = datetime.datetime.fromtimestamp(b_time)
        try:
            return b_time.strftime(time_format)
        except TypeError as exc:
            raise SaltInvocationError('Invalid format string: {0}'.format(exc))
    return b_time
Example #8
0
File: web.py Project: 5n1p/psdash
def index():
    load_avg = os.getloadavg()
    uptime = datetime.now() - datetime.fromtimestamp(psutil.get_boot_time())
    disks = get_disks()
    users = get_users()

    netifs = get_network_interfaces()
    netifs.sort(key=lambda x: x.get("bytes_sent"), reverse=True)

    data = {
        "os": platform.platform().decode("utf-8"),
        "hostname": socket.gethostname().decode("utf-8"),
        "uptime": str(uptime).split(".")[0],
        "load_avg": load_avg,
        "cpus": psutil.NUM_CPUS,
        "vmem": psutil.virtual_memory(),
        "swap": psutil.swap_memory(),
        "disks": disks,
        "cpu_percent": psutil.cpu_times_percent(0),
        "users": users,
        "net_interfaces": netifs,
        "page": "overview",
        "is_xhr": request.is_xhr
    }

    return render_template("index.html", **data)
Example #9
0
def boot_time():
    """
    Get system boot time
    :return:
    """
    b_time = psutil.get_boot_time()
    return datetime.datetime.fromtimestamp(b_time).strftime(
        "%Y-%m-%d %H:%M:%S")
Example #10
0
def boot():
    try:
        boot_time = psutil.boot_time()
    except AttributeError:
        boot_time = psutil.get_boot_time()
        # Compatible < 2.0.0
    has_boot = time.time() - boot_time
    hour = int(has_boot / 3600)
    return str(hour) + ':' + str(int((has_boot - hour * 3600) / 60))
Example #11
0
def index():
    uptime = datetime.now() - datetime.fromtimestamp(psutil.get_boot_time())
    return render_template('system/system.html',
            uptime = str(uptime).split('.')[0],
            load = os.getloadavg(),
            net = psutil.net_io_counters(),
            memory = psutil.virtual_memory(),
            swap = psutil.swap_memory(),
        )
Example #12
0
def boot():
    try:
        boot_time = psutil.boot_time()
    except AttributeError:
        boot_time = psutil.get_boot_time()
        # Compatible < 2.0.0
    has_boot = time.time() - boot_time
    hour = int(has_boot / 3600)
    return str(hour) + ':' + str(int((has_boot - hour * 3600) / 60))
Example #13
0
def index():
    uptime = datetime.now() - datetime.fromtimestamp(psutil.get_boot_time())
    return render_template(
        'system/system.html',
        uptime=str(uptime).split('.')[0],
        load=os.getloadavg(),
        net=psutil.net_io_counters(),
        memory=psutil.virtual_memory(),
        swap=psutil.swap_memory(),
    )
Example #14
0
 def uptime(self):
     try:
         d = {}
         boot = datetime.now() - datetime.fromtimestamp(psutil.get_boot_time())
         boot = str(boot)
         uptime = boot[:-7]
         d['uptime'] = uptime
         return json.dumps(d)
     except Exception as e:
         self.logger.error("Could not get uptime %s" % e)
Example #15
0
File: ps.py Project: talwai/salt
def boot_time():
    '''
    Return the boot time in number of seconds since the epoch began.

    CLI Example:

    .. code-block:: bash

        salt '*' ps.boot_time
    '''
    return psutil.get_boot_time()
Example #16
0
File: ps.py Project: bemehow/salt
def boot_time():
    '''
    Return the boot time in number of seconds since the epoch began.

    CLI Example:

    .. code-block:: bash

        salt '*' ps.boot_time
    '''
    return psutil.get_boot_time()
Example #17
0
 def uptime(self):
     try:
         if psutil.version_info >= (2, 0, 0):
             b = psutil.boot_time()
         else:
             b = psutil.get_boot_time()
         d = {}
         boot = datetime.now() - datetime.fromtimestamp(b)
         boot = str(boot)
         uptime = boot[:-7]
         d['uptime'] = uptime
         return json.dumps(d)
     except Exception as e:
         self.logger.error("Could not get uptime %s" % e)
Example #18
0
 def uptime(self):
     try:
         if psutil.version_info >= (2, 0, 0):
             b = psutil.boot_time()
         else:
             b = psutil.get_boot_time()
         d = {}
         boot = datetime.now() - datetime.fromtimestamp(b)
         boot = str(boot)
         uptime = boot[:-7]
         d['uptime'] = uptime
         return json.dumps(d)
     except Exception as e:
         self.logger.error("Could not get uptime %s" % e)
Example #19
0
    def sysinfo(self, facts):

        now = datetime.datetime.now()

        try:
            btime = datetime.datetime.fromtimestamp(psutil.get_boot_time())
        except AttributeError:
            # old psutil version
            btime = datetime.datetime.fromtimestamp(psutil.BOOT_TIME)

        #facts["hostname"] = socket.getfqdn()
        facts["hostname"] = socket.gethostname()
        facts["os"] = platform.platform()
        facts["kernel"] = platform.release()
        facts["cpus"] = psutil.NUM_CPUS
        facts["uptime"] = str(now - btime).split(".")[0]
Example #20
0
def fetch():
    #
    ldict = {"step": MODULEFILE + "/" + inspect.stack()[0][3],
             "hostname": platform.node().split(".")[0]}
    l = logging.LoggerAdapter(fetch_lg(), ldict)
    #
    l.info("Gathering host info...")
    host_props = {}
    host_props["machine"] = platform.machine()
    host_props["arch"] = platform.machine()
    host_props["release"] = platform.release()
    host_props["system"] = platform.system()
    host_props["hostname"] = platform.node()
    host_props["name"] = host_props["hostname"]
    host_props["domainname"] = ".".join(socket.gethostname().split('.')[1:])
    host_props["fqdn"] = socket.gethostname()
    host_props["memory"] = psutil.virtual_memory().total
    host_props["memory_used"] = psutil.virtual_memory().percent
    host_props["cpu"] = psutil.NUM_CPUS
    dt = datetime.datetime.fromtimestamp
    host_props["boottime"] = dt(psutil.get_boot_time())
    host_props["cpu_used"] = psutil.cpu_percent(interval=1, percpu=False)
    host_props["hardwareisa"] = platform.processor()
    host_props["hardwaremodel"] = platform.processor()
    host_props["user"] = os.environ['USER']
    ostype = os.uname()[0]
    if ostype == "Linux":
        host_props["kernel_release"] = platform.uname()[2]
        host_props["kernel_version"] = platform.uname()[2].split('-')[0]
        try:
            host_props_extra = fetch_linux()
        except Exception as _:
            if sillyfacter.config.STRICT:
                raise
            if sillyfacter.config.DEBUG:
                l.exception("")
            l.error("unable to fetch linux specific info '{}'".format(_))
            host_props_extra = {}
        host_props = dict(host_props.items() + host_props_extra.items())
    for i in host_props:
        l.debug("{:<15}: {}".format(i, host_props[i]))
    return host_props
Example #21
0
 def get_uptime_minutes(self):
     """Get the system uptime in seconds"""
     boot_time = None
     try:
         boot_time = psutil.boot_time()
     except Exception:
         pass
     if boot_time is None:
         try:
             boot_time = psutil.get_boot_time()
         except Exception:
             pass
     if boot_time is None:
         try:
             boot_time = psutil.BOOT_TIME
         except Exception:
             pass
     uptime = None
     if boot_time is not None and boot_time > 0:
         uptime = int((time.time() - boot_time) / 60)
     if uptime is not None and uptime < 0:
         uptime = 0
     return uptime
Example #22
0
    def index(self):

        bandwidth = getBandwidth()

        transmission, couchpotato, sickbeard, headphones = get_program_status()

        tmpl = env.get_template('index.html')
        return tmpl.render(
            # sys info
            cpu        = psutil.cpu_percent(),
            ram        = psutil.virtual_memory().percent,
            bootTime   = psutil.get_boot_time(),
            usedDisk   = convertBytes(psutil.disk_usage('/').used),
            totalDisk  = psutil.disk_usage('/').total >> 30,
            monthlyBW  = {{MONTHLY_BANDWIDTH}},
            currTime   = str(datetime.datetime.now()),
            # Charts
            hourly_tx  = bandwidth['hourly_tx'],
            hourly_rx  = bandwidth['hourly_rx'],
            daily_tx   = bandwidth['daily_tx'],
            daily_rx   = bandwidth['daily_rx'],
            monthly_tx = bandwidth['monthly_tx'],
            monthly_rx = bandwidth['monthly_rx'],
            totalAll   = bandwidth['totalAll'],
            totalMonth = bandwidth['totalMonth'],
            # Maintenance
            transmissionIsOn = transmission,
            couchpotatoIsOn  = couchpotato,
            sickbeardIsOn    = sickbeard,
            headphonesIsOn   = headphones,
            # Streaming Media
            tv     = get_directory_structure('/home/tv'),
            music  = get_directory_structure('/home/music'),
            other  = get_directory_structure('/home/other'),
            movies = get_directory_structure('/home/movies')
        )
##      -- psutil from Giampaolo Rodola' --             ##
##########################################################
import psutil

__version__ = "ver. 1.0 - beta"
__date__ = 2013, 7, 28

print("-- for help type -- info --")
new = input(" what do you wanna know about the system?: ")
##############################################################################
## memory ##
memo = psutil.virtual_memory()
swap = psutil.swap_memory()
## systm ##
user = psutil.get_users()
boot = psutil.get_boot_time()
syst = psutil.get_pid_list()
sysl = psutil.get_process_list()
## cpu ##
cpus = psutil.NUM_CPUS
cpuc = psutil.cpu_percent(interval=1, percpu=True)
cpuv = psutil.cpu_times(percpu=True)
## network ##
netw = psutil.network_io_counters()
nets = psutil.network_io_counters(pernic=True)
## disk ##
dska = psutil.disk_partitions(all=False)
dskb = psutil.disk_io_counters(perdisk=False)
dskm = psutil.disk_io_counters(perdisk=True)
dskc = psutil.disk_usage("C:")
dskd = psutil.disk_usage("D:")
Example #24
0
def runtime_info():
    info = dict()
    info['uptime'] = psutil.get_boot_time()
    return info
def output_sys_y(sysy):
  return psutil.get_boot_time()
__date__ = 2013, 8, 3

#####################################################
## cpu ##
cpus = psutil.NUM_CPUS
cpul = psutil.cpu_percent(interval=1, percpu=True)
cpuv = psutil.cpu_times(percpu=True)
## memory ##
mema = psutil.virtual_memory()
memw = psutil.swap_memory()
## disk ##
dskc = psutil.disk_usage("/root/")
dskd = psutil.disk_usage("/")
## systm ##
sysz = psutil.get_users()
sysy = psutil.get_boot_time()
sysr = psutil.get_pid_list()
sysh = psutil.get_process_list()
## network ##
netn = psutil.network_io_counters()
netm = psutil.network_io_counters(pernic=True)
#####################################################
def print_options():
  print("Options:")
  print("  CPU info's")
  print("    'l' cpu load in %")
  print("    's' cpu's in systm")
  print("    'v' cpu load")
  print("  RAM info's")
  print("    'a' ram info")
  print("    'w' swap info")
Example #27
0
def uptime():
    uptime = datetime.now() - datetime.fromtimestamp(psutil.get_boot_time())
    return str(uptime)
Example #28
0
def boot():
    has_boot = time.time() - psutil.get_boot_time()
    hour = int(has_boot / 3600)
    return str(hour) + ':' + str(int((has_boot - hour * 3600) / 60))
Example #29
0
 def status_uptime(self):
     return time.time() - psutil.get_boot_time()
Example #30
0
def upTime():
	try:
		return time.time()-psutil.boot_time()
	except:
		return time.time()-psutil.get_boot_time()
Example #31
0
def getUptime():
        bootTime = int(ps.get_boot_time())
        return int(getTimestamp() - bootTime)
Example #32
0
 def status_uptime(self):
     return time.time() - psutil.get_boot_time()
Example #33
0
		if diff_out_p < 0:
			diff_out_p = 0
			
		old_net_data[nic] = data
		
		sock.send(msgpack.packb({
			"service": "machine",
			"msg_type": "value",
			"resource_type": "network",
			"unit": nic,
			"values": {
				"bps_in": diff_in_b / interval,
				"bps_out": diff_out_b / interval,
				"pps_in": diff_in_p / interval,
				"pps_out": diff_out_p / interval
			}
		}))
		
	sock.send(msgpack.packb({
		"service": "machine",
		"msg_type": "value",
		"resource_type": "uptime",
		"unit": "",
		"values": {
			"uptime": time.time() - psutil.get_boot_time()
		}
	}))
	
	time.sleep(interval)

Example #34
0
def fetch2json(f, fetchable):
    ldict = {"step": MODULEFILE + "/" + inspect.stack()[0][3],
             "hostname": platform.node().split(".")[0]}
    l = logging.LoggerAdapter(fetch_lg(), ldict)
    #
    myfslookup, mynslookup = lookup(f)
    fetched = {}
    fetched["_scan_id"] = platform.node()
    fetched["_scan_time"] = datetime.datetime.now()
    fetched["has"] = {}
    fetched["had"] = {}
    dt = datetime.datetime.fromtimestamp
    boottime = dt(psutil.get_boot_time())
    # host main
    if "os" in f:
        for item, val in f["os"].iteritems():
            fetched[item] = val

    # special
    if "custom" in f:
        pass

    # host networks
    if "network" in f:
        fetched["has"]["network"] = []
        for item in f["network"]:
            newitem = {}
            newitem["ifname"] = item["ifname"]
            newitem["ip"] = item["ip"]
            #if newitem["ip"] in f["nslookup"]:
            #    newitem["hostname"] = f["nslookup"][newitem["ip"]]
            fetched["has"]["network"].append(newitem)

    # host procs
    if "process" in f:
        fetched["has"]["process"] = []
        for pid in f["process"]:
            _process_type = set()
            newitem = {}
            newitem["pid"] = pid
            newitem["commandline"] = f["process"][pid]["cmdline"]
            newitem["exe"] = f["process"][pid]["exe"]
            if "exe_fs" in f["process"][pid]:
                newitem["exe_fs"] = f["process"][pid]["exe_fs"]
            newitem["user"] = f["process"][pid]["user"]
            newitem["createtime"] = f["process"][pid]["create_time"]
            if boottime + datetime.timedelta(seconds=600) \
                    > newitem["createtime"]:
                _process_type.add("startup-prog")
            else:
                _process_type.add("user-prog")
            #
            # proc network conn
            if "connection" in f["process"][pid]:
                newitem["connection"] = []
                newconns = f["process"][pid]["connection"]
                for conn in newconns:
                    conn_src = conn[0]
                    conn_src_ip = ":".join(conn_src.split(':')[:-1])
                    conn_src_port = ":".join(conn_src.split(':')[-1:])
                    conn_src_host = conn_src_ip
                    conn_dst = conn[1]
                    if conn_dst is not None:
                        conn_dst_ip = ":".join(conn_dst.split(':')[:-1])
                        conn_dst_port = ":".join(conn_dst.split(':')[-1:])
                        conn_dst_host = conn_dst_ip
                        _process_type_network = "network-client"
                    else:
                        conn_dst_port = "*"
                        conn_dst_ip = "0.0.0.0"
                        conn_dst_host = "*"
                        _process_type_network = "network-daemon"
                    conn_pair = {"source": conn_src_host + ":" + conn_src_port,
                                 "destination": conn_dst_host +
                                 ":" + conn_dst_port}
                    newitem["connection"].append(conn_pair)
                    _process_type.add(_process_type_network)
                if len(newitem["connection"]) == 0:
                    del newitem["connection"]
            #
            # proc open mounts
            if "open" in f["process"][pid]:
                newitem["open"] = []
                newitem["fs"] = []
                open_fs = set()
                mountpairs = f["process"][pid]["open"]
                for mountpair in mountpairs:
                    if "path" in mountpair and "fs" in mountpair:
                        fs = mountpair["fs"]
                        open_fs.add(fs)
                        _, fstype = myfslookup(dev=fs)
                        if fstype is not None and len(fstype) > 0:
                            _process_type.add("{}-client".format(fstype))
                        fl = mountpair["path"]
                        newitem["open"].append({"file": fl,
                                                "filesystem": fs})
                open_fs_list = list(open_fs)
                if len(open_fs_list) > 0:
                    newitem["open_filesystem"] = open_fs_list

            newitem["type"] = list(_process_type)
            fetched["has"]["process"].append(newitem)
    if "user" in f:
        users = f["user"]
        if "current" in users:
            fetched["has"]["user"] = users["current"]
        if "last" in users:
            fetched["had"]["user"] = users["last"]
    #
    if "custom" in f:
        fetched_custom = {}
        for modulename, val in f["custom"].iteritems():
            try:
                fetched_custom = None
                m = fetchable["custom"][modulename]
                if hasattr(m, "output_for_json"):
                    fetched_custom = getattr(m, "output_for_json")(val)
                elif hasattr(m, "output"):
                    fetched_custom = getattr(m, "output")(val)
                else:
                    l.error("Could not find outout handler in module " +
                            "'{}'".format(m))
            except:
                l.error("custom module {} not loaded".format(modulename))
            else:
                if fetched_custom is not None:
                    fetched = dict(fetched, **fetched_custom)
    return fetched
Example #35
0
def get_boot_time():
    return psutil.get_boot_time()
Example #36
0
 def boot_time():
     # psutil-2.x.x is not backward compatible to psutil-1.x.x
     try:
         return psutil.boot_time()
     except AttributeError:
         return psutil.get_boot_time()
Example #37
0
def boot():
    has_boot = time.time() - psutil.get_boot_time()
    hour = int(has_boot / 3600)
    return str(hour) + ':' + str(int((has_boot - hour * 3600) / 60))
Example #38
0
	def boot_time():
		# psutil-2.x.x is not backward compatible to psutil-1.x.x
		try: return psutil.boot_time()
		except AttributeError: return psutil.get_boot_time()
Example #39
0
    def load_data(self):

        """
        Check System health status along with internet connectivity status and POST health check data after every seconds.
        
        """
        flag = 0
        device_id = self.get_device_id()

        while True:             

            try:
                firmware_usage = self.get_utilization("tracker*.zip")
            except:
                print "firmware is not running"
            else:
                print "Firmware usage:"+str(firmware_usage)

            app_usage = self.get_utilization("gateway_monitor*.zip")

            print "Monitoring app usage:"+json.dumps(app_usage)

            if self.internet_on():

                if self.token is None:
                    self.token = self.get_api_token()
                if flag >= 1:
                    flag = 0
                    to_timestamp = datetime.utcnow()

                    log = {
                        "device_id":device_id,
                        "from_timestamp":from_timestamp.strftime("%Y-%m-%d %I:%M:%S %p"),
                        "to_timestamp":to_timestamp.strftime("%Y-%m-%d %I:%M:%S %p"),
                        "status" : "Connection Lost",
                    }
                    log =  json.dumps(log,indent=4)
                    # print log

                    try:
                        requests.post(''.join([self.url,"/lrs/api/v1.0/gateway/internet/log"]),data=log,headers={'Content-Type':'application/json'}, auth=(self.token,''),verify=False,timeout=10) 
                    except:
                        pass

                cpu_utilization = psutil.cpu_percent()
                boot_time = datetime.utcfromtimestamp(psutil.get_boot_time())      
                uptime = datetime.now() - boot_time
                uptime = int(uptime.total_seconds())
                memstats = psutil.virtual_memory()
                mem_utilization = ((memstats.total-memstats.free)/memstats.total)*100               
                temperature = self.get_cpu_temperature()                
                firmware_status = self.firmware_status()        

                status = {
                    "timestamp":datetime.utcnow().strftime("%Y-%m-%d %I:%M:%S %p"),
                    "cpu_utilization":cpu_utilization,
                    "boot_time":boot_time.strftime("%Y-%m-%d %I:%M:%S %p"),
                    "uptime":uptime,
                    "mem_utilization":round(mem_utilization,2),
                    "temperature":temperature,
                    "device_id" : device_id,
                    "firmware_status":firmware_status
                }

                data =  json.dumps(status,indent=4)
                # print data

                try:
                    resp = requests.post(''.join([self.url,"/lrs/api/v1.0/statastic"]),data=data,headers={'Content-Type':'application/json'}, auth=(self.token,''),verify=False,timeout=10) 
                except requests.exceptions.Timeout:        
                    pass
                except requests.exceptions.TooManyRedirects:
                    sys.exit(1)
                except requests.exceptions.RequestException as e:
                    print e
                    sys.exit(1)

            elif flag == 0:
                flag = 1
                from_timestamp = datetime.utcnow()
            else:
                flag += 1                
            tm.sleep(10)