Ejemplo n.º 1
0
    def getpartitions(self):
        self.syslog.debug("Getting filesystem list ")
        parser = TeddixParser.TeddixStringParser() 

        i = 0 
        disks = { }
        mounts = psutil.disk_partitions() 
        for part in psutil.disk_partitions():
            
            # get addtional info 
            if part.mountpoint:
                usage = psutil.disk_usage(part.mountpoint)

                fstotal = parser.str2uni(usage.total)
                fsused = parser.str2uni(usage.used)
                fsfree = parser.str2uni(usage.free)
                fspercent = parser.str2uni(usage.percent)
    
            fsdev = parser.str2uni(part.device)
            fsmount = parser.str2uni(part.mountpoint)
            fstype = parser.str2uni(part.fstype)
            fsopts = parser.str2uni(part.opts)

            disks[i] = [fsdev,fsmount,fstype,fsopts,fstotal,fsused,fsfree,fspercent]
            i += 1

        return disks
Ejemplo n.º 2
0
def fetch():
    ldict = {"step": MODULEFILE + "/" + inspect.stack()[0][3],
             "hostname": platform.node().split(".")[0]}
    l = logging.LoggerAdapter(fetch_lg(), ldict)
    #
    l.info("Gathering filesystem info...")
    fs = []
    for p in psutil.disk_partitions(all=False):
        l.info("Found {} mounted as {}".format(p.device, p.mountpoint))
        partition = {}
        partition = {'dev': p.device,
                     "mount": p.mountpoint,
                     "fstype": p.fstype}
        if (re.match('ext[0-9]|xfs|ufs|btrfs', p.fstype, re.IGNORECASE)):
            try:
                disk_usage = psutil.disk_usage(p.mountpoint)
            except:
                if sillyfacter.config.STRICT:
                    raise
                if sillyfacter.config.DEBUG:
                    l.exception("")
            else:
                partition["total"] = disk_usage.total
                partition["used"] = disk_usage.used
                partition["free"] = disk_usage.free
                partition["percent"] = disk_usage.percent
        fs.append(partition)
    for p in psutil.disk_partitions(all=True):
        if (re.match('nfs|autofs', p.fstype, re.IGNORECASE)):
            l.info("Found '{}' mounted as '{}'".format(p.device, p.mountpoint))
            fs.append(
                {'dev': p.device, "mount": p.mountpoint, "fstype": p.fstype})
    return fs
Ejemplo n.º 3
0
def main():


    while True:
        psutil.disk_partitions(all=False)
        templ = "%5s%%"
        usage = psutil.disk_usage('C:\\')
        print(templ %(int(usage.percent)))
        if usage.percent > 94:

            sender = '*****@*****.**'
            receivers = '*****@*****.**'

            msg = MIMEMultipart()
            msg['From'] = '*****@*****.**'
            msg['To'] = '*****@*****.**'
            msg['Subject'] = 'Check space Disk C:\ for %s' %socket.gethostname()
            message = 'Attenzione spazio su disco C:\ insufficiente %s'%(templ%(usage.percent))
            msg.attach(MIMEText(message))
            try:
                smtpObj = smtplib.SMTP('xxx.yyy.zz',NN)
                smtpObj.sendmail(sender, receivers, msg.as_string())
                print ("Successfully sent email")
                print(socket.gethostname())
            except SMTPException:
                print("Error: unable to send email")
        time.sleep(300)
        continue
Ejemplo n.º 4
0
    def test_disk_partitions(self):
        # all = False
        ls = psutil.disk_partitions(all=False)
        # on travis we get:
        #     self.assertEqual(p.cpu_affinity(), [n])
        # AssertionError: Lists differ: [0, 1, 2, 3, 4, 5, 6, 7,... != [0]
        self.assertTrue(ls, msg=ls)
        for disk in ls:
            if WINDOWS and 'cdrom' in disk.opts:
                continue
            if not POSIX:
                assert os.path.exists(disk.device), disk
            else:
                # we cannot make any assumption about this, see:
                # http://goo.gl/p9c43
                disk.device
            if SUNOS:
                # on solaris apparently mount points can also be files
                assert os.path.exists(disk.mountpoint), disk
            else:
                assert os.path.isdir(disk.mountpoint), disk
            assert disk.fstype, disk
            self.assertIsInstance(disk.opts, str)

        # all = True
        ls = psutil.disk_partitions(all=True)
        self.assertTrue(ls, msg=ls)
        for disk in psutil.disk_partitions(all=True):
            if not WINDOWS:
                try:
                    os.stat(disk.mountpoint)
                except OSError as err:
                    if TRAVIS and OSX and err.errno == errno.EIO:
                        continue
                    # http://mail.python.org/pipermail/python-dev/
                    #     2012-June/120787.html
                    if err.errno not in (errno.EPERM, errno.EACCES):
                        raise
                else:
                    if SUNOS:
                        # on solaris apparently mount points can also be files
                        assert os.path.exists(disk.mountpoint), disk
                    else:
                        assert os.path.isdir(disk.mountpoint), disk
            self.assertIsInstance(disk.fstype, str)
            self.assertIsInstance(disk.opts, str)

        def find_mount_point(path):
            path = os.path.abspath(path)
            while not os.path.ismount(path):
                path = os.path.dirname(path)
            return path.lower()

        mount = find_mount_point(__file__)
        mounts = [x.mountpoint.lower() for x in
                  psutil.disk_partitions(all=True)]
        self.assertIn(mount, mounts)
        psutil.disk_usage(mount)
Ejemplo n.º 5
0
 def DISK(self):
   ps=0
   pt=[]
   while ps < len(psutil.disk_partitions()):
     for i in range(3):
       pt.append(psutil.disk_partitions()[ps][i])
     pt.append(str(psutil.disk_usage(psutil.disk_partitions()[ps][1])[-1])+'%')
     ps+=1
   return json.dumps({"Partitions": len(psutil.disk_partitions()), "DiskInfo": pt})
Ejemplo n.º 6
0
def test_number_of_partitions(flush):
    expected_all_partitions = psutil.disk_partitions(True)  # All partitions
    expected_phy_partitions = psutil.disk_partitions(False)  # Physical only

    actual_all_partitions = P.disk_partitions(0)
    actual_phy_partitions = P.disk_partitions(1)

    assert actual_all_partitions.nitems == len(expected_all_partitions)
    assert actual_phy_partitions.nitems == len(expected_phy_partitions)
Ejemplo n.º 7
0
 def get_value(self):
     if self.variant == 'total':
         u, f, n = 0, 0, 0
         for x in psutil.disk_partitions():
             d = psutil.disk_usage(x.mountpoint)
             u += d.used
             f += d.free
             n += 1
         return ((float(u)/float(f))*100)/n
     else:
         return psutil.disk_usage(next(x.mountpoint for x in psutil.disk_partitions() if x.device == self.variant)).percent
Ejemplo n.º 8
0
 def __init__(self):
     self.devices = [disk.device for disk in psutil.disk_partitions()]
     self.mountpoint = [disk.mountpoint for disk in psutil.disk_partitions()]
     self.fstype = [disk.fstype for disk in psutil.disk_partitions()]
     self.device_type = [self.GetDriveType(path) for path in self.devices]
     self.os_name = self.get_os_name()
     for path in self.mountpoint:
         self.total.append(bytes2mega(psutil.disk_usage(path).total))
         self.used.append(bytes2human(psutil.disk_usage(path).used))
         self.free.append(bytes2human(psutil.disk_usage(path).free))
         self.percent.append(str(psutil.disk_usage(path).percent) + "%")
Ejemplo n.º 9
0
def getDetails():

    # return these details
    deets = {"cpu_usage":None,"memory_usage":None,"disk_usage":None,"os_version":platform.platform()}

    deets['cpu_usage'] = psutil.cpu_percent(interval=None)
    deets['memory_usage'] = psutil.virtual_memory().percent
    deets['disk_usage'] = psutil.disk_usage('/').percent

    ####
    # Bonus data:
    ####

    # CPU
    deets['cpu_details'] = {'details':[]}
    cpu_details = psutil.cpu_percent(interval=None, percpu=True)
    deets['cpu_details']['count'] = len(cpu_details)
    for i in range(0,len(cpu_details)):
        deets['cpu_details']['details'].append({'name':i,'cpu_usage':cpu_details[i]})

    # Memory
    deets['memory_details'] = {}
    deets['memory_details']['virtual'] = {
        'percent':psutil.virtual_memory().percent,
        'total':psutil.virtual_memory().total
        }
    deets['memory_details']['swap'] = {
        'percent':psutil.swap_memory().percent,
        'total':psutil.swap_memory().total
        }

    # Disk
    deets['disk_details'] = {}
    deets['disk_details']['partition_count'] = len(psutil.disk_partitions())
    deets['disk_details']['partition_count'] = len(psutil.disk_partitions())
    deets['disk_details']['partitions'] = []
    for partition in psutil.disk_partitions():
        deets['disk_details']['partitions'].append({
            'device':partition.device,
            'mountpoint':partition.mountpoint,
            'percent':psutil.disk_usage(partition.mountpoint).percent,
            'total':psutil.disk_usage(partition.mountpoint).total,
            })
    deets['disk_details']['iostats'] = {}
    sdiskio = psutil.disk_io_counters()
    for k in ['read_count','write_count','read_bytes','write_bytes','read_time','write_time']:
        deets['disk_details']['iostats'][k] = getattr(sdiskio,k)

    deets['hostname'] = platform.node()
    deets['boottime'] = datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
    
    return deets    
Ejemplo n.º 10
0
def get_disk_usage():
    disks = []
    devices = len(psutil.disk_partitions())

    for i in range(devices):
        disk_device_data = {}
        path = psutil.disk_partitions()[i].mountpoint
        disk_device_data["device"] = path
        disk_device_data["percent"] = psutil.disk_usage(path).percent

        disks.append(disk_device_data)

    return disks
Ejemplo n.º 11
0
    def collect_disks(self):
        psutil.disk_partitions()
        partitions = psutil.disk_partitions(all=False)

        results = {}
        for part in partitions:
            usage = psutil.disk_usage(part.mountpoint)
            _, _, disk = part.device.rpartition("/")
            results['disk.%s.percent' % disk] = usage.percent
            results['disk.%s.total' % disk] = usage.total
            results['disk.%s.used' % disk] = usage.used
            results['disk.%s.free' % disk] = usage.free

        return results
Ejemplo n.º 12
0
    def test_disks(self):
        # test psutil.disk_usage() and psutil.disk_partitions()
        # against "df -a"
        def df(path):
            out = sh('df -k "%s"' % path).strip()
            lines = out.split('\n')
            lines.pop(0)
            line = lines.pop(0)
            dev, total, used, free = line.split()[:4]
            if dev == 'none':
                dev = ''
            total = int(total) * 1024
            used = int(used) * 1024
            free = int(free) * 1024
            return dev, total, used, free

        for part in psutil.disk_partitions(all=False):
            usage = psutil.disk_usage(part.mountpoint)
            dev, total, used, free = df(part.mountpoint)
            self.assertEqual(part.device, dev)
            self.assertEqual(usage.total, total)
            # 10 MB tollerance
            if abs(usage.free - free) > 10 * 1024 * 1024:
                self.fail("psutil=%s, df=%s" % usage.free, free)
            if abs(usage.used - used) > 10 * 1024 * 1024:
                self.fail("psutil=%s, df=%s" % usage.used, used)
Ejemplo n.º 13
0
Archivo: web.py Proyecto: 5n1p/psdash
def get_disks(all_partitions=False):
    disks = [
        (dp, psutil.disk_usage(dp.mountpoint))
        for dp in psutil.disk_partitions(all_partitions)
    ]
    disks.sort(key=lambda d: d[1].total, reverse=True)
    return disks
Ejemplo n.º 14
0
    def __update__(self):
        """
        Update the stats
        """

        # Reset the list
        self.fs_list = []

        # Open the current mounted FS
        fs_stat = psutil.disk_partitions(all=True)
        for fs in range(len(fs_stat)):
            fs_current = {}
            fs_current["device_name"] = fs_stat[fs].device
            if fs_current["device_name"] in self.ignore_fsname:
                continue
            fs_current["fs_type"] = fs_stat[fs].fstype
            if fs_current["fs_type"] in self.ignore_fstype:
                continue
            fs_current["mnt_point"] = fs_stat[fs].mountpoint
            if fs_current["mnt_point"] in self.ignore_mntpoint:
                continue
            try:
                fs_usage = psutil.disk_usage(fs_current["mnt_point"])
            except Exception as e:
                LOG.warning(str(e))
                continue
            fs_current["size"] = fs_usage.total
            fs_current["used"] = fs_usage.used
            fs_current["avail"] = fs_usage.free
            self.fs_list.append(fs_current)
Ejemplo n.º 15
0
def main():

    hostname = getfqdn()
    data = ''

    for k, v in psutil.cpu_times_percent(interval=0.5)._asdict().iteritems():
        data += '{0}.cpu.{1} value={2}\n'.format(hostname, k, v)

    for k, v in psutil.virtual_memory()._asdict().iteritems():
        data += '{0}.mem.{1} value={2}\n'.format(hostname, k, v)

    for k, v in psutil.swap_memory()._asdict().iteritems():
        data += '{0}.swap.{1} value={2}\n'.format(hostname, k, v)

    du = { mp.mountpoint: psutil.disk_usage(mp.mountpoint).percent for mp in psutil.disk_partitions() }
    for k, v in du.iteritems():
        data += '{0}.du_percent.{1} value={2}\n'.format(hostname, k, v)

    for k, v in psutil.disk_io_counters(perdisk=True).iteritems():
        for m, p in v._asdict().iteritems():
            data += '{0}.io.{1}.{2} value={3}\n'.format(hostname, k, m, p)

    for k, v in psutil.net_io_counters(pernic=True).iteritems():
        for m, p in v._asdict().iteritems():
            data += '{0}.net.{1}.{2} value={3}\n'.format(hostname, k, m, p)

    connections = map(lambda x: x.status.lower(), psutil.net_connections())
    connections = map( lambda x: 'unknown_state' if x == 'none' else x, connections )
    for conn in set(connections):
        data += '{0}.netstat.{1} value={2}\n'.format(hostname, conn, connections.count(conn))

    if DEBUG: print data
    sendData(data)
Ejemplo n.º 16
0
    def __set_bases(self):
        """
        Calculate base stats for I/O at start of the node.
        Used to calculate difference to get per/s stats.
        """
        psutil.cpu_percent()
        psutil.cpu_percent(percpu=True)

        for interface in psutil.net_io_counters(True):

            netint = psutil.net_io_counters(True)[interface]
            total_bytes = netint.bytes_recv + netint.bytes_sent
            total_packages = netint.packets_sent + netint.packets_recv

            self.__bandwidth_base[interface] = total_bytes
            self.__msg_freq_base[interface] = total_packages

        dev_names = []
        for disk in psutil.disk_partitions():
            if all(['cdrom' not in disk.opts, 'sr' not in disk.device]):
                dev_names.append(disk.device)

        for key in psutil.disk_io_counters(True):
            if key in dev_names:
                disk = psutil.disk_io_counters(True)[key]
                self.__disk_read_base[key] = disk.read_bytes
                self.__disk_write_base[key] = disk.write_bytes
Ejemplo n.º 17
0
    def __measure_disk_usage(self):
        """
        measure current disk usage
        """
        # Free Space on disks
        disks = psutil.disk_partitions()
        dev_name = []
        for disk in disks:
            if 'cdrom' not in disk.opts and 'sr' not in disk.device:
                free_space = psutil.disk_usage(disk.mountpoint).free / 2 ** 20
                self._status.add_drive_space(disk.device, free_space)
                dev_name.append(disk.device)

        # Drive I/O
        drive_io = psutil.disk_io_counters(True)

        for disk in dev_name:
            if disk in drive_io:
                readb = drive_io[disk].read_bytes - self.__disk_read_base[disk]
                writeb = drive_io[disk].write_bytes - \
                    self.__disk_write_base[disk]

                read_rate = readb / self.__update_interval
                write_rate = writeb / self.__update_interval
                self._status.add_drive_read(disk, read_rate)
                self._status.add_drive_write(disk, write_rate)
                # update base stats for next iteration
                self.__disk_read_base[disk] += readb
                self.__disk_write_base[disk] += writeb
            else:
                # No information available - push None's
                self._status.add_drive_read(disk, None)
                self._status.add_drive_write(disk, None)
Ejemplo n.º 18
0
    def check(self, instance):
        btrfs_devices = {}
        excluded_devices = instance.get('excluded_devices', [])
        for p in psutil.disk_partitions():
            if (p.fstype == 'btrfs' and p.device not in btrfs_devices
                    and p.device not in excluded_devices):
                btrfs_devices[p.device] = p.mountpoint

        if len(btrfs_devices) == 0:
            raise Exception("No btrfs device found")

        for device, mountpoint in btrfs_devices.iteritems():
            for flags, total_bytes, used_bytes in self.get_usage(mountpoint):
                replication_type, usage_type = FLAGS_MAPPER[flags]
                tags = [
                    'usage_type:{0}'.format(usage_type),
                    'replication_type:{0}'.format(replication_type),
                ]

                free = total_bytes - used_bytes
                usage = float(free) / float(total_bytes)

                self.gauge('system.disk.btrfs.total', total_bytes, tags=tags, device_name=device)
                self.gauge('system.disk.btrfs.used', used_bytes, tags=tags, device_name=device)
                self.gauge('system.disk.btrfs.free', free, tags=tags, device_name=device)
                self.gauge('system.disk.btrfs.usage', usage, tags=tags, device_name=device)
Ejemplo n.º 19
0
def validate_volume(mount_path):
    """Determine if the volume is a valid Quobyte mount.

    Runs a number of tests to be sure this is a (working) Quobyte mount
    """
    partitions = psutil.disk_partitions(all=True)
    for p in partitions:
        if mount_path != p.mountpoint:
            continue
        if p.device.startswith("quobyte@") or p.fstype == "fuse.quobyte":
            statresult = os.stat(mount_path)
            # Note(kaisers): Quobyte always shows mount points with size 0
            if statresult.st_size == 0:
                # client looks healthy
                return  # we're happy here
            else:
                msg = (_("The mount %(mount_path)s is not a "
                         "valid Quobyte volume. Stale mount?")
                       % {'mount_path': mount_path})
            raise nova_exception.StaleVolumeMount(msg, mount_path=mount_path)
        else:
            msg = (_("The mount %(mount_path)s is not a valid "
                     "Quobyte volume according to partition list.")
                   % {'mount_path': mount_path})
            raise nova_exception.InvalidVolume(msg)
    msg = (_("No matching Quobyte mount entry for %(mount_path)s"
             " could be found for validation in partition list.")
           % {'mount_path': mount_path})
    raise nova_exception.InvalidVolume(msg)
Ejemplo n.º 20
0
def get_disk_info(sensor_list):
    disks = [psutil.disk_usage(s.mountpoint) for s in psutil.disk_partitions()]
    hdd_temps = [s for s in sensor_list if (s.SensorType == u'Temperature' and u'hdd' in s.Parent.lower())]
    return u'\002Storage\002: %.2fTB Used (%.2fTB Total) [%.2f°C (%.2f°C)]' % (round(bytes_to_tbytes(sum([d.used for d in disks])), 2),
                                                                               round(bytes_to_tbytes(sum([d.total for d in disks])), 2),
                                                                               round(avg(float(h.Value) for h in hdd_temps), 2),
                                                                               round(avg(float(h.Max) for h in hdd_temps), 2))
Ejemplo n.º 21
0
Archivo: base.py Proyecto: mdavid/packs
def check_diskio():
    dm = False
    disk_map = {}
    try:
        # total io counters
        diskio_all = psutil.disk_io_counters()
        for k, v in diskio_all._asdict().iteritems():
            disk_map["disk." + k] = v
        # per disk io counters
        diskio_per_disk = psutil.disk_io_counters(perdisk=True)
        for device, details in diskio_per_disk.iteritems():
            for k, v in diskio_per_disk[device]._asdict().iteritems():
                disk_map["disk." + device.lower() + "." + k] = v
    except RuntimeError:  # Windows needs disk stats turned on with 'diskperf -y'
        pass
    # check for any device mapper partitions
    for partition in psutil.disk_partitions():
        if '/dev/mapper' in partition.device:
            dm = True
    # per device mapper friendly name io counters
    if dm:
        device_mapper = {}
        for name in os.listdir('/dev/mapper'):
            path = os.path.join('/dev/mapper', name)
            if os.path.islink(path):
                device_mapper[os.readlink(os.path.join('/dev/mapper', name)).replace('../', '')] = name
        for device, details in diskio_per_disk.iteritems():
            for k, v in diskio_per_disk[device]._asdict().iteritems():
                if device in device_mapper:
                    disk_map["disk." + device_mapper[device] + "." + k] = v
    return disk_map
Ejemplo n.º 22
0
def process(param):
    pack = MapPack()
    pack.putStr("_name_", "Disk Usage")
    deviceLv = pack.newList("Device")
    totalLv = pack.newList("Total")
    usedLv = pack.newList("Used")
    freeLv = pack.newList("Free")
    useLv = pack.newList("Use")
    typeLv = pack.newList("Type")
    mountLv = pack.newList("Mount")
    for part in psutil.disk_partitions(all=False):
        if os.name == 'nt':
            if 'cdrom' in part.opts or part.fstype == '':
                # skip cd-rom drives with no disk in it; they may raise
                # ENOENT, pop-up a Windows GUI error for a non-ready
                # partition or just hang.
                continue
        usage = psutil.disk_usage(part.mountpoint)
        deviceLv.addStr(part.device)
        totalLv.addStr(bytes2human(usage.total))
        usedLv.addStr(bytes2human(usage.used))
        freeLv.addStr(bytes2human(usage.free))
        useLv.addStr(str(usage.percent) + "%")
        typeLv.addStr(part.fstype)
        mountLv.addStr(part.mountpoint)
    return pack
Ejemplo n.º 23
0
    def get_disk_usage(self, path_list):
        _columns = ('total', 'used', 'free')
        data = {}

        partition_list = psutil.disk_partitions(all=True)

        def _sanitize(p):
            if len(p) > 1:
                return p.rstrip("/")
            return p
        path_list = map(_sanitize, path_list)

        for path in path_list:
            try:
                part = filter(lambda x: x.mountpoint == path, partition_list)[0]
                if os.name == 'nt':
                    if 'cdrom' in part.opts or part.fstype == '':
                        # skip cd-rom drives with no disk in it; they may raise
                        # ENOENT, pop-up a Windows GUI error for a non-ready
                        # partition or just hang.
                        continue
                usage = psutil.disk_usage(part.mountpoint)
                row = dict(zip(_columns, map(lambda x: x / (1024 * 1024), usage)))  # Convert to MB
                row['volume'] = part.device
                row['path'] = part.mountpoint
                row['percent'] = int(usage.percent)

                data[part.device] = row
            except IndexError:
                pass

        return data
Ejemplo n.º 24
0
    def __update__(self):
        """
        Update the stats
        """
        # Reset the list
        self.fs_list = []

        # Open the current mounted FS
        fs_stat = psutil.disk_partitions(all=True)
        for fs in range(len(fs_stat)):
            fs_current = {}
            fs_current['device_name'] = fs_stat[fs].device
            if fs_current['device_name'] in self.ignore_fsname:
                continue
            fs_current['fs_type'] = fs_stat[fs].fstype
            if fs_current['fs_type'] in self.ignore_fstype:
                continue
            fs_current['mnt_point'] = fs_stat[fs].mountpoint
            if fs_current['mnt_point'] in self.ignore_mntpoint:
                continue
            try:
                fs_usage = psutil.disk_usage(fs_current['mnt_point'])
            except Exception:
                continue
            fs_current['size'] = fs_usage.total
            fs_current['used'] = fs_usage.used
            fs_current['avail'] = fs_usage.free
            self.fs_list.append(fs_current)
Ejemplo n.º 25
0
def get_fs(path='.'):
    """ Gets info about the filesystem on which `path` lives. """
    mount = find_mount_point(path)

    for fs in psutil.disk_partitions(True):
        if fs.mountpoint == mount:
            return fs
Ejemplo n.º 26
0
 def test_disks(self):
     ps_parts = psutil.disk_partitions(all=True)
     wmi_parts = wmi.WMI().Win32_LogicalDisk()
     for ps_part in ps_parts:
         for wmi_part in wmi_parts:
             if ps_part.device.replace('\\', '') == wmi_part.DeviceID:
                 if not ps_part.mountpoint:
                     # this is usually a CD-ROM with no disk inserted
                     break
                 try:
                     usage = psutil.disk_usage(ps_part.mountpoint)
                 except OSError:
                     err = sys.exc_info()[1]
                     if err.errno == errno.ENOENT:
                         # usually this is the floppy
                         break
                     else:
                         raise
                 self.assertEqual(usage.total, int(wmi_part.Size))
                 wmi_free = int(wmi_part.FreeSpace)
                 self.assertEqual(usage.free, wmi_free)
                 # 10 MB tollerance
                 if abs(usage.free - wmi_free) > 10 * 1024 * 1024:
                     self.fail("psutil=%s, wmi=%s" % (
                         usage.free, wmi_free))
                 break
         else:
             self.fail("can't find partition %s" % repr(ps_part))
Ejemplo n.º 27
0
def sysinfo():
    boot_start = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(psutil.boot_time()))
    time.sleep(0.5)
    cpu_usage = psutil.cpu_percent()
    ram = int(psutil.virtual_memory().total / (1027 * 1024))
    ram_percent = psutil.virtual_memory().percent
    swap = int(psutil.swap_memory().total / (1027 * 1024))
    swap_percent = psutil.swap_memory().percent
    net_sent = psutil.net_io_counters().bytes_sent
    net_recv = psutil.net_io_counters().bytes_recv
    net_spkg = psutil.net_io_counters().packets_sent
    net_rpkg = psutil.net_io_counters().packets_recv
    sysinfo_string = ""
    if __name__ == "__main__":
        bfh = r'%'
        print(" \033[1;32m开机时间:%s\033[1;m" % boot_start)
        print(" \033[1;32m当前CPU使用率:%s%s\033[1;m" % (cpu_usage, bfh))
        print(" \033[1;32m物理内存:%dM\t使用率:%s%s\033[1;m" % (ram, ram_percent, bfh))
        print(" \033[1;32mSwap内存:%dM\t使用率:%s%s\033[1;m" % (swap, swap_percent, bfh))
        print(" \033[1;32m发送:%d Byte\t发送包数:%d个\033[1;m" % (net_sent, net_spkg))
        print(" \033[1;32m接收:%d Byte\t接收包数:%d个\033[1;m" % (net_recv, net_rpkg))
        for i in psutil.disk_partitions():
            print(" \033[1;32m盘符: %s 挂载点: %s 使用率: %s%s\033[1;m" % (i[0], i[1], psutil.disk_usage(i[1])[3], bfh))

        sysinfo_string = '开机时间: ' + boot_start + '\n'
        sysinfo_string += '当前CPU使用率: ' + str(cpu_usage) + '%\n'
        sysinfo_string += '物理内存: ' + str(ram) + 'M\t使用率: ' + str(ram_percent) + '%\t'
        sysinfo_string += 'Swap内存: ' + str(swap) + 'M\t使用率: ' + str(swap_percent) + '%\n'
        return sysinfo_string

    else:
        file = open("sysinfo.log", "a")
        file.write("CPU:%s   \tRAM:%s\tnet_recv:%d\tNet_sent:%d\r\n" % (cpu_usage, ram_percent, net_recv, net_sent))
        file.flush()
        file.close()
Ejemplo n.º 28
0
    def serialize(self):

        parts = psutil.disk_partitions()
        partitions = []

        # get --bind mounts
        bind_mounts = []
        exitcode, stdout, stderr = shlaunch("findmnt -nr | fgrep [ | cut -d' ' -f1")
        if exitcode == 0:
            bind_mounts = stdout

        for part in parts:
            if 'loop' not in part.device and part.mountpoint not in bind_mounts:
                usage = psutil.disk_usage(part.mountpoint)
                partitions.append({
                    'device': part.device,
                    'mountpoint': part.mountpoint,
                    'fstype': part.fstype,
                    'opts': part.opts,
                    'usage': {
                        'total': size_format(usage.total),
                        'used': size_format(usage.used),
                        'free': size_format(usage.free),
                        'percent': usage.percent
                    }
                })

        return {
            'partitions': partitions,
        }
Ejemplo n.º 29
0
def get_hdd():
    valid_fs = [
        "ext4",
        "ext3",
        "ext2",
        "reiserfs",
        "jfs",
        "btrfs",
        "fuseblk",
        "zfs",
        "simfs",
        "ntfs",
        "fat32",
        "exfat",
        "xfs",
    ]
    disks = dict()
    size = 0
    used = 0
    for disk in psutil.disk_partitions():
        if not disk.device in disks and disk.fstype.lower() in valid_fs:
            disks[disk.device] = disk.mountpoint
    for disk in disks.itervalues():
        usage = psutil.disk_usage(disk)
        size += usage.total
        used += usage.used
    return int(size / 1024.0 / 1024.0), int(used / 1024.0 / 1024.0)
Ejemplo n.º 30
0
def show_disks():
	disks = psutil.disk_partitions(all=True)
	for disk in disks:
		print("dev:", disk.device)
		print("\tmount:", disk.mountpoint)
		print("\tfstype:", disk.fstype)
		print("\toptions:", disk.opts)
Ejemplo n.º 31
0
def collect():
    '''

    **Purpose:**

    The collect function collects performance information about XFERO and the
    server it is running on. It monitors the following and produced logs.

    *CPU*

    *psutil.cpu_percent(interval=0.1, percpu=False)*

    Return a float representing the current system-wide CPU utilization as a
    percentage. When interval is > 0.0 compares system CPU times elapsed before
    and after the interval (blocking). When interval is 0.0 or None compares
    system CPU times elapsed since last call or module import, returning
    immediately. In this case is recommended for accuracy that this function be
    called with at least 0.1 seconds between calls.

    When percpu is True returns a list of floats representing the utilization as
    a percentage for each CPU. First element of the list refers to first CPU,
    second element to second CPU and so on. The order of the list is consistent
    across calls.

    *psutil.cpu_times(percpu=False)*

    Return system CPU times as a namedtuple. Every attribute represents the time
    CPU has spent in the given mode.

    The attributes availability varies depending on the platform. Here follows a
    list of all available attributes:

    .. hlist::

       * user
       * system
       * idle
       * nice (UNIX)
       * iowait (Linux)
       * irq (Linux, FreeBSD)
       * softirq (Linux)
       * steal (Linux >= 2.6.11)
       * guest (Linux >= 2.6.24)
       * guest_nice (Linux >= 3.2.0)

    *psutil.cpu_times_percent(interval=0.1, percpu=False)*

    Same as cpu_percent() but provides utilization percentages for each specific
    CPU time as is returned by cpu_times(). interval and percpu arguments have
    the same meaning as in cpu_percent().

    *MEMORY*

    *psutil.virtual_memory()*

    Return statistics about system memory usage as a namedtuple including the
    following fields, expressed in bytes:

    .. hlist::

       * total: total physical memory available
       * available: the actual amount of available memory that can be given
       instantly to processes that request more memory in bytes; this is
       calculated by summing different memory values depending on the platform
       (e.g. free + buffers + cached on Linux) and it is supposed to be used to
       monitor actual memory usage in a cross platform fashion.
       * percent: the percentage usage calculated as (total - available) / total
       x 100
       * used: memory used, calculated differently depending on the platform and
       designed for informational purposes only.
       * free: memory not being used at all (zeroed) that is readily available;
       note that this doesn't reflect the actual memory available (use
       'available' instead).

    Platform-specific fields:

    .. hlist::

       * active (UNIX): memory currently in use or very recently used, and so it
       is in RAM.
       * inactive (UNIX): memory that is marked as not used.
       * buffers (Linux, BSD): cache for things like file system metadata.
       * cached (Linux, BSD): cache for various things.
       * wired (BSD, OSX): memory that is marked to always stay in RAM. It is
       never moved to disk.
       * shared (BSD): memory that may be simultaneously accessed by multiple
       processes.

    The sum of 'used' and 'available' does not necessarily equal total. On
    Windows 'available' and 'free' are the same.

    *psutil.swap_memory()*

    Return system swap memory statistics as a named tuple including the
    following attributes:

    .. hlist::
       * total: total swap memory in bytes
       * used: used swap memory in bytes
       * free: free swap memory in bytes
       * percent: the percentage usage
       * sin: no. of bytes the system has swapped in from disk (cumulative)
       * sout: no. of bytes the system has swapped out from disk (cumulative)

    'sin' and 'sout' on Windows are meaningless and always set to 0.

    *DISK*

    *psutil.disk_partitions(all=False)*

    Return all mounted disk partitions as a list of namedtuples including
    device, mount point and filesystem type, similarly to "df" command on posix.

    If all parameter is False return physical devices only (e.g. hard disks,
    cd-rom drives, USB keys) and ignore all others (e.g. memory partitions such
    as /dev/shm).

    Namedtuple's 'fstype' field is a string which varies depending on the
    platform.

    On Linux it can be one of the values found in /proc/filesystems (e.g. 'ext3'
    for an ext3 hard drive o 'iso9660' for the CD-ROM drive).

    On Windows it is determined via GetDriveType and can be either "removable",
    "fixed", "remote", "cdrom", "unmounted" or "ramdisk".

    On OSX and FreeBSD it is retrieved via getfsstat(2).

    *psutil.disk_usage(path)*

    Return disk usage statistics about the given path as a namedtuple including
    total, used and free space expressed in bytes plus the percentage usage.
    OSError is raised if path does not exist.

    *psutil.disk_io_counters(perdisk=False)*

    Return system disk I/O statistics as a namedtuple including the following
    attributes:

    .. hlist::

       * read_count: number of reads
       * write_count: number of writes
       * read_bytes: number of bytes read
       * write_bytes: number of bytes written
       * read_time: time spent reading from disk (in milliseconds)
       * write_time: time spent writing to disk (in milliseconds)

    If perdisk is True return the same information for every physical disk
    installed on the system as a dictionary with partition names as the keys and
    the named tuple described above as the values.

    *Network*

    *psutil.net_io_counters(pernic=False)*

    Return network I/O statistics as a namedtuple including the following
    attributes:

    .. hlist::
       * bytes_sent: number of bytes sent
       * bytes_recv: number of bytes received
       * packets_sent: number of packets sent
       * packets_recv: number of packets received
       * errin: total number of errors while receiving
       * errout: total number of errors while sending
       * dropin: total number of incoming packets which were dropped
       * dropout: total number of outgoing packets which were dropped (always 0
       on OSX and BSD)

    If pernic is True return the same information for every network interface
    installed on the system as a dictionary with network interface names as the
    keys and the named tuple described above as the values.

    **Usage Notes:**

    None

    *Example usage:*

    ```collect()```

    **Process Flow**

    .. figure::  ../process_flow/stats.png
       :align:   center

       Process Flow: Stats

    *External dependencies*

    os (/xfero/.stats.xfero_stats)
    psutil (/xfero/.stats.xfero_stats)
    /xfero/
      get_conf (/xfero/.stats.xfero_stats)

    +------------+-------------+-----------------------------------------------+
    | Date       | Author      | Change Details                                |
    +============+=============+===============================================+
    | 02/12/2013 | Chris Falck | Created                                       |
    +------------+-------------+-----------------------------------------------+
    | 27/10/2014 | Chris Falck | modified call to get_conf                     |
    +------------+-------------+-----------------------------------------------+

    '''
    try:
        (xfero_logger,.xfero_database, outbound_directory, transient_directory,
         error_directory, xfero_pid) = get_conf.get.xfero_config()
    except Exception as err:
        print('Cannot get XFERO Config: %s', err)
        raise err

    logging.config.fileConfig(xfero_logger)
    # create logger
    logger = logging.getLogger('sysinfo')

    logger.info('Running XFERO Scheduler...')

    # Capture CPU info
    logger.info(10 * '*' + ' CPU INFORMATION ' + 10 * '*')
    num_cpus = psutil.NUM_CPUS
    logger.info('Number of CPU\'s = %s', num_cpus)

    logger.info('CPU Times:')
    logger.info(psutil.cpu_times())

    logger.info(
        'Current System-wide CPU utilization as a percentage per CPU. 5 x \
        Responses with 3 second interval')
    for element in range(5):
        logger.info(psutil.cpu_percent(interval=3, percpu=True))

    logger.info(
        'CPU Times as a Percentage per CPU. Every attribute represents the \
        time a CPU has spent in the given mode. 5 x Responses with 3 second \
        interval')
    for element in range(5):
        logger.info(psutil.cpu_times_percent(interval=3, percpu=True))

    # Capture Memory info
    logger.info(10 * '*' + ' MEMORY INFORMATION ' + 10 * '*')
    logger.info('Virtual Memory:')
    logger.info(psutil.virtual_memory())
    logger.info('Swap Memory:')
    logger.info(psutil.swap_memory())

    # Capture Disk info
    logger.info(10 * '*' + ' DISK INFORMATION ' + 10 * '*')
    logger.info('Disk Partitions:')
    logger.info(psutil.disk_partitions())
    logger.info('Disk Usage:')
    logger.info(psutil.disk_usage('/'))
    logger.info('IO Counters')
    logger.info(psutil.disk_io_counters())

    # Capture Network info
    logger.info(10 * '*' + ' NETWORK INFORMATION PER NIC ' + 10 * '*')
    logger.info(psutil.net_io_counters(pernic=True))
Ejemplo n.º 32
0
    def check(self, instance):
        """Capture disk stats

        """
        dimensions = self._set_dimensions(None, instance)
        rollup_dimensions = dimensions.copy()

        if instance is not None:
            use_mount = instance.get("use_mount", True)
            send_io_stats = instance.get("send_io_stats", True)
            send_rollup_stats = instance.get("send_rollup_stats", False)
            # If we filter devices, get the list.
            device_blacklist_re = self._get_re_exclusions(instance)
            fs_types_to_ignore = self._get_fs_exclusions(instance)
        else:
            use_mount = True
            send_io_stats = True
            send_rollup_stats = False
            device_blacklist_re = None
            fs_types_to_ignore = []

        partitions = psutil.disk_partitions(all=True)
        if send_io_stats:
            disk_stats = psutil.disk_io_counters(perdisk=True)
        disk_count = 0
        total_capacity = 0
        total_used = 0
        for partition in partitions:
            if partition.fstype not in fs_types_to_ignore \
                and (not device_blacklist_re
                     or not device_blacklist_re.match(partition.device)):
                    device_name = self._get_device_name(partition.device)
                    disk_usage = psutil.disk_usage(partition.mountpoint)
                    total_capacity += disk_usage.total
                    total_used += disk_usage.used
                    st = os.statvfs(partition.mountpoint)
                    if use_mount:
                        dimensions.update({'mount_point': partition.mountpoint})
                    self.gauge("disk.space_used_perc",
                               disk_usage.percent,
                               device_name=device_name,
                               dimensions=dimensions)
                    disk_count += 1
                    if st.f_files > 0:
                        self.gauge("disk.inode_used_perc",
                                   round((float(st.f_files - st.f_ffree) / st.f_files) * 100, 2),
                                   device_name=device_name,
                                   dimensions=dimensions)
                        disk_count += 1

                    log.debug('Collected {0} disk usage metrics for partition {1}'.format(disk_count, partition.mountpoint))
                    disk_count = 0
                    if send_io_stats:
                        try:
                            stats = disk_stats[device_name]
                            self.rate("io.read_req_sec", round(float(stats.read_count), 2), device_name=device_name, dimensions=dimensions)
                            self.rate("io.write_req_sec", round(float(stats.write_count), 2), device_name=device_name, dimensions=dimensions)
                            self.rate("io.read_kbytes_sec", round(float(stats.read_bytes / 1024), 2), device_name=device_name, dimensions=dimensions)
                            self.rate("io.write_kbytes_sec", round(float(stats.write_bytes / 1024), 2), device_name=device_name, dimensions=dimensions)
                            self.rate("io.read_time_sec", round(float(stats.read_time / 1000), 2), device_name=device_name, dimensions=dimensions)
                            self.rate("io.write_time_sec", round(float(stats.write_time / 1000), 2), device_name=device_name, dimensions=dimensions)

                            log.debug('Collected 6 disk I/O metrics for partition {0}'.format(partition.mountpoint))
                        except KeyError:
                            log.debug('No Disk I/O metrics available for {0}...Skipping'.format(device_name))

        if send_rollup_stats:
            self.gauge("disk.total_space_mb",
                       total_capacity / 1048576,
                       dimensions=rollup_dimensions)
            self.gauge("disk.total_used_space_mb",
                       total_used / 1048576,
                       dimensions=rollup_dimensions)
            log.debug('Collected 2 rolled-up disk usage metrics')
Ejemplo n.º 33
0
def action():
    checkPlatform()
    print("-" * 40, "Sys Info", "-" * 40)
    uname = platform.uname()
    print(f"System:                                 {uname.system}")
    print(f"Node name:                              {uname.node}")
    print(f"Release:                                {uname.release}")
    print(f"Version:                                {uname.version}")
    print(f"Machine:                                {uname.machine}")
    print(f"Processor:                              {uname.processor}")

    # BOOT TIME
    print("-" * 40, "Boot time", "-" * 39)
    boot_time_timestamp = psutil.boot_time()
    bt = datetime.fromtimestamp(boot_time_timestamp)
    print(
        f"Boot time:                              {bt.day}.{bt.month}.{bt.year} {bt.hour}:{bt.minute}.{bt.second}"
    )

    # CPU INFO
    print("-" * 40, "CPU info", "-" * 40)
    print("Actual Cores:                          ",
          psutil.cpu_count(logical=False))
    print("Logical Cores:                         ",
          psutil.cpu_count(logical=True))
    print(
        f"Max Frequency:                          {psutil.cpu_freq().current:.1f}Mhz"
    )
    print(
        f"Current Frequency:                      {psutil.cpu_freq().current:.1f}Mhz"
    )
    print(f"CPU usage:                              {psutil.cpu_percent()}%")
    print("CPU usage/core:")
    for i, perc in enumerate(psutil.cpu_percent(percpu=True, interval=1)):
        print(f"\tCore {i}:                         {perc}%")

    def adjust_size(size):
        factor = 1024
        for i in ["B", "KB", "MB", "GB", "TB"]:
            if size > factor:
                size = size / factor
            else:
                return f"{size:.3f}{i}"

    # RAM INFO
    print("-" * 40, "RAM info", "-" * 40)
    virtual_mem = psutil.virtual_memory()
    print(
        f"Total:                                  {adjust_size(virtual_mem.total)}"
    )
    print(
        f"Available:                              {adjust_size(virtual_mem.available)}"
    )
    print(
        f"Used:                                   {adjust_size(virtual_mem.used)}"
    )
    print(f"Percentage:                             {virtual_mem.percent}%")
    print("-" * 20, "SWAP", "-" * 20)
    swap = psutil.swap_memory()
    print(f"Total:                                  {adjust_size(swap.total)}")
    print(f"Free:                                   {adjust_size(swap.free)}")
    print(f"Used:                                   {adjust_size(swap.used)}")
    print(f"Percentage:                             {swap.percent}%")

    # DISK INFO
    print("-" * 40, "Disk info", "-" * 39)
    partitions = psutil.disk_partitions()
    for p in partitions:
        print(f"Device:                                 {p.device}")
        print(f"\tMountpoint:                     {p.mountpoint}")
        print(f"\tFile system type:               {p.fstype}")
        try:
            partitions_usage = psutil.disk_usage(p.mountpoint)
        except PermissionError:
            continue
        print(
            f"Total size:                             {adjust_size(partitions_usage.total)}"
        )
        print(
            f"Used:                                   {adjust_size(partitions_usage.used)}"
        )
        print(
            f"Free:                                   {adjust_size(partitions_usage.free)}"
        )
        print(
            f"Percentage:                             {partitions_usage.percent}%"
        )
    disk_io = psutil.disk_io_counters()
    print(
        f"Read since boot:                        {adjust_size(disk_io.read_bytes)}"
    )
    print(
        f"Written since boot:                     {adjust_size(disk_io.write_bytes)}"
    )

    # GPU INFO
    print("-" * 40, "GPU info", "-" * 40)
    gpus = GPUtil.getGPUs()
    for gpu in gpus:
        print(
            f"ID:                                     {gpu.id}, Name: {gpu.name}"
        )
        print(f"\tLoad:                           {gpu.load * 100}%")
        print(f"\tFree Mem:                       {gpu.memoryFree}MB")
        print(f"\tUsed Mem:                       {gpu.memoryUsed}MB")
        print(f"\tTotal Mem:                      {gpu.memoryTotal}MB")
        print(f"\tTemperature:                    {gpu.temperature} °C")

    # NETWORK INFO
    print("-" * 40, "Network info", "-" * 36)
    if_addrs = psutil.net_if_addrs()
    for interface_name, interface_addresses in if_addrs.items():
        for address in interface_addresses:
            print(f"Interface:                              {interface_name}")
            if str(address.family) == 'AddressFamily.AF_INET':
                print(f"\tIP Address:                     {address.address}")
                print(f"\tNetmask:                        {address.netmask}")
                print(f"\tBroadcast IP:                   {address.broadcast}")
            elif str(address.family) == 'AddressFamily.AF_PACKET':
                print(f"\tMAC Address:              {address.address}")
                print(f"\tNetmask:                      {address.netmask}")
                print(f"\tBroadcast MAC:            {address.broadcast}")
    net_io = psutil.net_io_counters()
    print(
        f"Total Bytes send:                       {adjust_size(net_io.bytes_sent)}"
    )
    print(
        f"Total Bytes received:                   {adjust_size(net_io.bytes_recv)}"
    )
    print(
        f"Total Packets send:                     {adjust_size(net_io.packets_sent)}"
    )
    print(
        f"Total Packts received:                  {adjust_size(net_io.packets_recv)}"
    )
    print(f"Total errors while receiving:           {net_io.errin}")
    print(f"Total errors while sending:             {net_io.errout}")
    print(f"Incoming packets which were dropped:    {net_io.dropin}")
    print(f"Outgoing packets which were dropped:    {net_io.dropout}")

    # BATTERY INFO
    print("-" * 40, "Battery info", "-" * 36)
    print(f"Sensor battery:")
    print(f"\t{psutil.sensors_battery()}")
    print()
Ejemplo n.º 34
0
    def collect_system_utilization(self):
        values = {}

        values['cpu'] = psutil.cpu_percent(interval=0.2, percpu=True)
        mem = psutil.virtual_memory()
        values['memory'] = mem.percent
        values['disks'] = {}
        values['jobs'] = [
            self.max_parallel_jobs,
            len(self.queue),
            len(self.job_processes)
        ]
        values['nets'] = {}
        values['processes'] = []

        for disk in psutil.disk_partitions():
            name = self.get_disk_name(disk[1])
            values['disks'][name] = psutil.disk_usage(disk[1]).used

        net_stats = psutil.net_io_counters(pernic=True)
        for id in self.nets:
            net = net_stats[id]
            values['nets'][id] = {
                'recv': net.bytes_recv,
                'sent': net.bytes_sent,
                'upload': 0,
                'download': 0
            }

            if id in self.last_net and self.last_utilization:
                values['nets'][id]['upload'] = (
                    net.bytes_sent - self.last_net[id]['sent']) / (
                        time.time() - self.last_utilization)
                values['nets'][id]['download'] = (
                    net.bytes_recv - self.last_net[id]['recv']) / (
                        time.time() - self.last_utilization)

            self.last_net[id] = dict(values['nets'][id])

        for p in psutil.process_iter():
            try:
                cpu = p.cpu_percent()
                if cpu > 1 or p.memory_percent() > 1:
                    values['processes'].append([
                        p.pid,
                        p.name(),
                        p.username(),
                        p.create_time(),
                        p.status(),
                        p.num_threads(),
                        p.memory_percent(), cpu
                    ])
            except OSError:
                pass
            except psutil.Error:
                pass

        try:
            values['loadavg'] = os.getloadavg()
        except OSError:
            pass

        self.last_utilization = time.time()
        return values
Ejemplo n.º 35
0
    def render(self, context):
        try:
            import psutil as pu
        except:
            context['error_psutil'] = 'not_found'
            return ''

        # cpu
        cpu_info = cpuTuple(
            core=pu.NUM_CPUS,
            used=pu.cpu_percent())

        # memory
        mem_info = memTuple(
            total=bytes2human(pu.TOTAL_PHYMEM),
            used=pu.virtual_memory().percent)

        # disk
        partitions = list()
        for part in pu.disk_partitions():
            partitions.append(
                diskPartTuple(
                    device=part.device,
                    mountpoint=part.mountpoint,
                    fstype=part.fstype,
                    total=bytes2human(
                        pu.disk_usage(part.mountpoint).total),
                    percent=pu.disk_usage(part.mountpoint).percent))

        # network
        networks = list()
        for k, v in pu.net_io_counters(pernic=True).items():
            # Skip loopback interface
            if k == 'lo':
                continue

            networks.append(
                networkTuple(
                    device=k,
                    sent=bytes2human(v.bytes_sent),
                    recv=bytes2human(v.bytes_recv),
                    pkg_sent=v.packets_sent,
                    pkg_recv=v.packets_recv))

        # processes
        processes = list()
        for process in pu.process_iter():

            try:
                percent = process.get_memory_percent()
            except AccessDenied:
                percent = "Access Denied"
            else:
                percent = int(percent)

            processes.append(processTuple(
                pid=process.pid,
                name=process.name,
                status=process.status,
                user=process.username,
                memory=percent))

        processes_sorted = sorted(
            processes, key=lambda p: p.memory, reverse=True)

        all_stats = {
            'cpu_info': cpu_info,
            'mem_info': mem_info,
            'partitions': partitions,
            'networks': networks,
            'processes': processes_sorted[:10],
        }

        context.update(all_stats)

        return ''
Ejemplo n.º 36
0
    def update(self):
        """Update the FS stats using the input method."""
        # Init new stats
        stats = self.get_init_value()

        if self.input_method == 'local':
            # Update stats using the standard system lib

            # Grab the stats using the psutil disk_partitions
            # If 'all'=False return physical devices only (e.g. hard disks, cd-rom drives, USB keys)
            # and ignore all others (e.g. memory partitions such as /dev/shm)
            try:
                fs_stat = psutil.disk_partitions(all=False)
            except (UnicodeDecodeError, PermissionError):
                return self.stats

            # Optionnal hack to allow logicals mounts points (issue #448)
            # Ex: Had to put 'allow=zfs' in the [fs] section of the conf file
            #     to allow zfs monitoring
            for fstype in self.get_conf_value('allow'):
                try:
                    fs_stat += [
                        f for f in psutil.disk_partitions(all=True)
                        if f.fstype.find(fstype) >= 0
                    ]
                except UnicodeDecodeError:
                    return self.stats

            # Loop over fs
            for fs in fs_stat:
                # Do not take hidden file system into account
                # Also check device name (see issue #1606)
                if self.is_hide(fs.mountpoint) or self.is_hide(fs.device):
                    continue
                # Grab the disk usage
                try:
                    fs_usage = psutil.disk_usage(fs.mountpoint)
                except OSError:
                    # Correct issue #346
                    # Disk is ejected during the command
                    continue
                fs_current = {
                    'device_name': fs.device,
                    'fs_type': fs.fstype,
                    # Manage non breaking space (see issue #1065)
                    'mnt_point': u(fs.mountpoint).replace(u'\u00A0', ' '),
                    'size': fs_usage.total,
                    'used': fs_usage.used,
                    'free': fs_usage.free,
                    'percent': fs_usage.percent,
                    'key': self.get_key()
                }
                stats.append(fs_current)

        elif self.input_method == 'snmp':
            # Update stats using SNMP

            # SNMP bulk command to get all file system in one shot
            try:
                fs_stat = self.get_stats_snmp(
                    snmp_oid=snmp_oid[self.short_system_name], bulk=True)
            except KeyError:
                fs_stat = self.get_stats_snmp(snmp_oid=snmp_oid['default'],
                                              bulk=True)

            # Loop over fs
            if self.short_system_name in ('windows', 'esxi'):
                # Windows or ESXi tips
                for fs in fs_stat:
                    # Memory stats are grabbed in the same OID table (ignore it)
                    if fs == 'Virtual Memory' or fs == 'Physical Memory' or fs == 'Real Memory':
                        continue
                    size = int(fs_stat[fs]['size']) * int(
                        fs_stat[fs]['alloc_unit'])
                    used = int(fs_stat[fs]['used']) * int(
                        fs_stat[fs]['alloc_unit'])
                    percent = float(used * 100 / size)
                    fs_current = {
                        'device_name': '',
                        'mnt_point': fs.partition(' ')[0],
                        'size': size,
                        'used': used,
                        'percent': percent,
                        'key': self.get_key()
                    }
                    # Do not take hidden file system into account
                    if self.is_hide(fs_current['mnt_point']):
                        continue
                    else:
                        stats.append(fs_current)
            else:
                # Default behavior
                for fs in fs_stat:
                    fs_current = {
                        'device_name': fs_stat[fs]['device_name'],
                        'mnt_point': fs,
                        'size': int(fs_stat[fs]['size']) * 1024,
                        'used': int(fs_stat[fs]['used']) * 1024,
                        'percent': float(fs_stat[fs]['percent']),
                        'key': self.get_key()
                    }
                    # Do not take hidden file system into account
                    if self.is_hide(fs_current['mnt_point']) or self.is_hide(
                            fs_current['device_name']):
                        continue
                    else:
                        stats.append(fs_current)

        # Update the stats
        self.stats = stats

        return self.stats
Ejemplo n.º 37
0
    def listdir(self):
        ret = []

        if self.path == "/gpvdmroot":
            itm = file_store()
            itm.file_name = "simulation_dir"
            itm.icon = "si"
            itm.display_name = _("Simulation")
            ret.append(itm)

            itm = file_store()
            itm.file_name = "home_dir"
            itm.icon = "user-home"
            itm.display_name = _("Home")
            ret.append(itm)

            if get_desktop_path() != False:
                itm = file_store()
                itm.file_name = "desktop_dir"
                itm.icon = "desktop"
                itm.display_name = _("Desktop")
                ret.append(itm)

            if get_downloads_path() != False:
                itm = file_store()
                itm.file_name = "downloads_dir"
                itm.icon = "folder-download"
                itm.display_name = _("Downloads")
                ret.append(itm)

            itm = file_store()
            itm.file_name = "gpvdm_configure"
            itm.icon = "cog"
            itm.display_name = _("Configure")
            ret.append(itm)

            for p in psutil.disk_partitions():
                name = p.mountpoint
                if running_on_linux() == True:
                    name = os.path.basename(name)

                if name == "":
                    name = "/"
                itm = file_store()
                itm.file_name = "mount_point::::" + p.mountpoint
                itm.icon = "drive-harddisk"
                itm.display_name = name
                ret.append(itm)
        elif self.path == "/gpvdmroot/gpvdm_configure":
            itm = file_store()
            itm.file_name = "gpvdm_cluster_config"
            itm.icon = "server"
            itm.display_name = _("Cluster")
            ret.append(itm)

            itm = file_store()
            itm.file_name = "gpvdm_language_config"
            itm.icon = "internet-chat"
            itm.display_name = _("Language")
            ret.append(itm)

            itm = file_store()
            itm.file_name = "gpvdm_solver_config"
            itm.icon = "accessories-calculator"
            itm.display_name = _("Solver")
            ret.append(itm)

            #itm=file_store()
            #itm.file_name="gpvdm_led_config"
            #itm.icon="oled"
            #itm.display_name=_("LED")
            #ret.append(itm)

            itm = file_store()
            itm.file_name = "gpvdm_dump_config"
            itm.icon = "hdd_custom"
            itm.display_name = _("Output files")
            ret.append(itm)

            itm = file_store()
            itm.file_name = "gpvdm_gui_config"
            itm.icon = "applications-interfacedesign"
            itm.display_name = _("GUI configuration")
            ret.append(itm)

            itm = file_store()
            itm.file_name = "gpvdm_server_config"
            itm.icon = "cpu"
            itm.display_name = _("Server")
            ret.append(itm)

            itm = file_store()
            itm.file_name = "gpvdm_key"
            itm.icon = "gnome-dialog-password"
            itm.display_name = _("License")
            ret.append(itm)

            itm = file_store()
            itm.file_name = "gpvdm_cache"
            itm.icon = "cache"
            itm.display_name = _("Cache")
            ret.append(itm)
        elif self.path.startswith(os.path.join(get_sim_path(),
                                               "parameters")) == True:
            from scan_human_labels import get_scan_human_labels
            s = get_scan_human_labels()
            s.ls_dir("/")

            itm = file_store()
            itm.file_name = "star"
            itm.icon = "star"
            itm.display_name = _("Cache")
            ret.append(itm)
        else:
            files = os.listdir(self.path)
            for f in files:
                itm = file_store()
                itm.file_name = f
                itm.isdir = os.path.isdir(os.path.join(self.path, f))
                itm.type = get_dir_type(os.path.join(self.path, f))
                if itm.type != "scan_dir":
                    ret.append(itm)

            #print(get_sim_path(),self.path)
            if get_sim_path() == self.path:
                itm = file_store()
                itm.file_name = "parameters"
                itm.type = "parameter_dir"
                itm.icon = "star"
                itm.display_name = _("parameters")
                ret.append(itm)

                scan = scans_io(get_sim_path())
                scans = scan.get_scans()
                for s in scans:
                    for i in range(0, len(ret)):
                        if ret[i].file_name == s.human_name:
                            ret.pop(i)
                            break

                    itm = file_store()
                    itm.type = "scan_dir"
                    itm.isdir = True
                    itm.file_name = os.path.basename(s.config_file)
                    itm.display_name = s.human_name
                    ret.append(itm)
        ret = sorted(ret, key=operator.attrgetter('display_name'))
        #files = sorted(files, key=operator.attrgetter('file_name'))
        return ret
Ejemplo n.º 38
0
    psutil.cpu_count(logical=False),  # CPU物理核心
    psutil.cpu_times()
)

# cpu使用频率
for x in range(10):
    print(psutil.cpu_percent(interval=1, percpu=True))

# 获取mem信息
print(
    psutil.virtual_memory(),  # 物理内存信息,单位整数字节
    psutil.swap_memory()  # 交换分区信息
)
# 获取磁盘信息
print(
    psutil.disk_partitions(),  # 磁盘分区信息
    psutil.disk_usage('c:\\'),  # 磁盘使用情况
    psutil.disk_io_counters()  # 磁盘IO
)

# 获取网络信息
print(
    psutil.net_io_counters(),  # 获取网络读写包个数
    psutil.net_if_addrs(),  # 获取网络接口信息
    psutil.net_if_stats(),  # 获取网络接口状态
    psutil.net_connections()  # 获取网络连接信息
)

# 获取进程信息
p = psutil.Process(3376)
print(
Ejemplo n.º 39
0
 def __init__(self):
     self.partitions = psutil.disk_partitions()
     self.last_io_counters = {}
     self.last_collect_time = datetime.datetime.now()
Ejemplo n.º 40
0
def beacon(config):
    r"""
    Monitor the disk usage of the minion

    Specify thresholds for each disk and only emit a beacon if any of them are
    exceeded.

    .. code-block:: yaml

        beacons:
          diskusage:
            - /: 63%
            - /mnt/nfs: 50%

    Windows drives must be quoted to avoid yaml syntax errors

    .. code-block:: yaml

        beacons:
          diskusage:
            -  interval: 120
            - 'c:\\': 90%
            - 'd:\\': 50%

    Regular expressions can be used as mount points.

    .. code-block:: yaml

        beacons:
          diskusage:
            - '^\/(?!home).*$': 90%
            - '^[a-zA-Z]:\\$': 50%

    The first one will match all mounted disks beginning with "/", except /home
    The second one will match disks from A:\ to Z:\ on a Windows system

    Note that if a regular expression are evaluated after static mount points,
    which means that if a regular expression matches another defined mount point,
    it will override the previously defined threshold.

    """
    parts = psutil.disk_partitions(all=True)
    ret = []
    for mounts in config:
        mount = next(iter(mounts))

        # Because we're using regular expressions
        # if our mount doesn't end with a $, insert one.
        mount_re = mount
        if not mount.endswith("$"):
            mount_re = "{0}$".format(mount)

        if salt.utils.platform.is_windows():
            # mount_re comes in formatted with a $ at the end
            # can be `C:\\$` or `C:\\\\$`
            # re string must be like `C:\\\\` regardless of \\ or \\\\
            # also, psutil returns uppercase
            mount_re = re.sub(r":\\\$", r":\\\\", mount_re)
            mount_re = re.sub(r":\\\\\$", r":\\\\", mount_re)
            mount_re = mount_re.upper()

        for part in parts:
            if re.match(mount_re, part.mountpoint):
                _mount = part.mountpoint

                try:
                    _current_usage = psutil.disk_usage(_mount)
                except OSError:
                    log.warning("%s is not a valid mount point.", _mount)
                    continue

                current_usage = _current_usage.percent
                monitor_usage = mounts[mount]
                if "%" in monitor_usage:
                    monitor_usage = re.sub("%", "", monitor_usage)
                monitor_usage = float(monitor_usage)
                if current_usage >= monitor_usage:
                    ret.append({"diskusage": current_usage, "mount": _mount})
    return ret
Ejemplo n.º 41
0
import psutil

#获取CPU信息
print(psutil.cpu_count()) #CPU路基数量
print(psutil.cpu_count(logical=False)) #CPU物理核心

print(psutil.cpu_times())

#for x in range(10):
#   print(psutil.cpu_percent(interval=1,percpu=True))


#获取内存信息
print(psutil.virtual_memory())
print(psutil.swap_memory())

#获取磁盘信息
print(psutil.disk_partitions()) #磁盘分区信息
print(psutil.disk_usage('/'))      #磁盘使用情况
print(psutil.disk_io_counters())     #磁盘IO
Ejemplo n.º 42
0
if _cpu_use_process_stat and 'MARS_CPU_TOTAL' in os.environ:
    _cpu_total = int(os.environ['MARS_CPU_TOTAL'].strip('"'))
else:
    _cpu_total = psutil.cpu_count(logical=True)

if _mem_use_process_stat and 'MARS_MEMORY_TOTAL' in os.environ:
    _mem_total = int(os.environ['MARS_MEMORY_TOTAL'].strip('"'))
else:
    _mem_total = None

_virt_memory_stat = namedtuple('virtual_memory',
                               'total available percent used free')

_shm_path = [
    pt.mountpoint for pt in psutil.disk_partitions(all=True)
    if pt.mountpoint in ('/tmp', '/dev/shm') and pt.fstype == 'tmpfs'
]
if not _shm_path:
    _shm_path = None
else:
    _shm_path = _shm_path[0]


def _read_cgroup_stat_file():
    with open(CGROUP_MEM_STAT_FILE, 'r') as cg_file:
        contents = cg_file.read()
    kvs = dict()
    for l in contents.splitlines():
        parts = l.split(' ')
        if len(parts) == 2:
Ejemplo n.º 43
0
    else:
        time_o = 5
    if p:
        try:
            percent = int(p[0].split('=')[-1])
        except ValueError:
            print("Please enter a valid percent integer")
    else:
        percent = 70

    timeout = start_time + datetime.timedelta(minutes=time_o)

    while datetime.datetime.now() < timeout:
        cpu = psutil.cpu_percent()
        memory = psutil.virtual_memory()[2]
        disk = psutil.disk_usage(psutil.disk_partitions()[0][0])[3]
        if cpu > percent:
            threshold = True
        if memory > percent:
            threshold = True
        if disk > percent:
            threshold = True

    if threshold:
        send_mail(cpu, memory, disk, email_user, email_pass, time_o)
else:
    print(f'{fname}: missing parameters')
    print(f'Try {fname} --help for more information')


Ejemplo n.º 44
0
def get_disks():
	parts = psutil.disk_partitions()
	return parts
Ejemplo n.º 45
0
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        info = cp.get_cpu_info()
        mem = psutil.virtual_memory()
        procs = {p.pid: p.info for p in psutil.process_iter(attrs=['pid'])}

        self.top_frame = tk.Frame(self, background="#f5f5f5", pady=10, padx=10)
        note = ttk.Notebook(self.top_frame)

        tab1 = ttk.Frame(note)
        tab2 = ttk.Frame(note)
        tab3 = ttk.Frame(note)

        cpu_label = ttk.Label(tab1, text="Vendor:")
        cpu_label.grid(row=0, column=0, pady=(5, 5))
        cpu_label1 = ttk.Label(tab1, text=info['brand'])
        cpu_label1.grid(row=0, column=1)

        cpu_label = ttk.Label(tab1, text="Processor:")
        cpu_label.grid(row=1, column=0, pady=(5, 5))
        cpu_label1 = ttk.Label(tab1, text=info['count'])
        cpu_label1.grid(row=1, column=1)

        cpu_label = ttk.Label(tab1, text="Architecture:")
        cpu_label.grid(row=2, column=0, pady=(5, 5))
        cpu_label1 = ttk.Label(tab1, text=info['arch'])
        cpu_label1.grid(row=2, column=1)

        cpu_label = ttk.Label(tab1, text="CPU count:")
        cpu_label.grid(row=3, column=0, pady=(5, 5))
        cpu_label1 = ttk.Label(tab1, text=info['count'])
        cpu_label1.grid(row=3, column=1)

        cpu_label = ttk.Label(tab1, text="No of process running:")
        cpu_label.grid(row=4, column=0, pady=(5, 5))
        cpu_label1 = ttk.Label(tab1, text=str(len(procs)))
        cpu_label1.grid(row=4, column=1)

        cpu_label = ttk.Label(tab1, text="uptime:")
        cpu_label.grid(row=5, column=0, pady=(5, 5))
        cpu_label1 = ttk.Label(tab1, text=str(psutil.cpu_times().user))
        cpu_label1.grid(row=5, column=1)

        cpu_label = ttk.Label(tab1, text="Cpu utilization:")
        cpu_label.grid(row=6, column=0, pady=(5, 5))
        cpu_label1 = ttk.Label(tab1,
                               text=str(psutil.cpu_percent(interval=1)) + "%")
        cpu_label1.grid(row=6, column=1)

        note.add(tab1, text="CPU")

        note.add(tab2, text="Memory")
        memory_label = ttk.Label(tab2, text="total")
        memory_label.grid(row=0, column=0, pady=(5, 5))
        memory_label1 = ttk.Label(tab2,
                                  text=str(bytes2human(mem.total)) + "mb")
        memory_label1.grid(row=0, column=1)

        memory_label = ttk.Label(tab2, text="Available")
        memory_label.grid(row=1, column=0, pady=(5, 5))
        memory_label1 = ttk.Label(tab2,
                                  text=str(bytes2human(mem.available)) + "mb")
        memory_label1.grid(row=1, column=1)

        memory_label = ttk.Label(tab2, text="used")
        memory_label.grid(row=2, column=0, pady=(5, 5))
        memory_label1 = ttk.Label(tab2, text=str(bytes2human(mem.used)) + "mb")
        memory_label1.grid(row=2, column=1)

        memory_label = ttk.Label(tab2, text="Free")
        memory_label.grid(row=3, column=0, pady=(5, 5))
        memory_label1 = ttk.Label(tab2, text=str(bytes2human(mem.free)) + "mb")
        memory_label1.grid(row=3, column=1)

        memory_label = ttk.Label(tab2, text="Active")
        memory_label.grid(row=4, column=0, pady=(5, 5))
        memory_label1 = ttk.Label(tab2,
                                  text=str(bytes2human(mem.active)) + "mb")
        memory_label1.grid(row=4, column=1)

        memory_label = ttk.Label(tab2, text="Inactive")
        memory_label.grid(row=5, column=0, pady=(5, 5))
        memory_label1 = ttk.Label(tab2,
                                  text=str(bytes2human(mem.inactive)) + "mb")
        memory_label1.grid(row=5, column=1)

        memory_label = ttk.Label(tab2, text="Buffers")
        memory_label.grid(row=6, column=0, pady=(5, 5))
        memory_label1 = ttk.Label(tab2,
                                  text=str(bytes2human(mem.buffers)) + "mb")
        memory_label1.grid(row=6, column=1)

        note.add(tab3, text="Disk")
        disk_label = ttk.Label(tab3,
                               text="Disk Usage",
                               font=("Helvetica", 12, 'bold'))
        disk_label.grid(row=0, column=0, pady=(5, 5), padx=(5, 5))
        disk_label1 = ttk.Label(tab3,
                                text="Total",
                                font=("Helvetica", 12, 'bold'))
        disk_label1.grid(row=0, column=1, pady=(5, 5), padx=(5, 5))
        disk_label2 = ttk.Label(tab3,
                                text="Used",
                                font=("Helvetica", 12, 'bold'))
        disk_label2.grid(row=0, column=2, pady=(5, 5), padx=(5, 5))
        disk_label3 = ttk.Label(tab3,
                                text="Free",
                                font=("Helvetica", 12, 'bold'))
        disk_label3.grid(row=0, column=3, pady=(5, 5), padx=(5, 5))
        disk_label4 = ttk.Label(tab3,
                                text="Used %",
                                font=("Helvetica", 12, 'bold'))
        disk_label4.grid(row=0, column=4, pady=(5, 5), padx=(5, 5))
        disk_label5 = ttk.Label(tab3,
                                text="Type",
                                font=("Helvetica", 12, 'bold'))
        disk_label5.grid(row=0, column=5, pady=(5, 5), padx=(5, 5))
        disk_label6 = ttk.Label(tab3,
                                text="Mount",
                                font=("Helvetica", 12, 'bold'))
        disk_label6.grid(row=0, column=6, pady=(5, 5), padx=(5, 5))

        row = 1
        for part in psutil.disk_partitions(all=False):
            if os.name == 'posix':
                if 'ext4' not in part.fstype:
                    print('this is working now')
                    continue
                if 'iso9660' in part.opts or part.fstype == '':
                    # skip cd-rom drives with no disk in it; they may raise
                    # ENOENT, pop-up a Windows GUI error for a non-ready
                    # partition or just hang.
                    continue
            usage = psutil.disk_usage(part.mountpoint)
            disk_label = ttk.Label(tab3, text=part.device)
            disk_label.grid(row=row, column=0, pady=(5, 5), padx=(5, 5))
            disk_label1 = ttk.Label(tab3, text=bytes2human(usage.total))
            disk_label1.grid(row=row, column=1, pady=(5, 5), padx=(5, 5))
            disk_label2 = ttk.Label(tab3, text=bytes2human(usage.used))
            disk_label2.grid(row=row, column=2, pady=(5, 5), padx=(5, 5))
            disk_label3 = ttk.Label(tab3, text=bytes2human(usage.free))
            disk_label3.grid(row=row, column=3, pady=(5, 5), padx=(5, 5))
            disk_label4 = ttk.Label(tab3, text=str(int(usage.percent)))
            disk_label4.grid(row=row, column=4, pady=(5, 5), padx=(5, 5))
            disk_label5 = ttk.Label(tab3, text=part.fstype)
            disk_label5.grid(row=row, column=5, pady=(5, 5), padx=(5, 5))
            disk_label6 = ttk.Label(tab3, text=part.mountpoint)
            disk_label6.grid(row=row, column=6, pady=(5, 5), padx=(5, 5))
            row = row + 1

        note.pack(expand=1, fill=tk.BOTH, padx=10, pady=10)

        self.top_frame.pack(expand=1, fill=tk.BOTH)
Ejemplo n.º 46
0
def getSystemInfo():
    try:
        # System Info
        info = {}
        info['platform'] = platform.system()
        info['platform-release'] = platform.release()
        info['platform-version'] = platform.version()
        info['architecture'] = platform.machine()
        info['hostname'] = socket.gethostname()
        info['ip-address'] = socket.gethostbyname(socket.gethostname())
        info['mac-address']=':'.join(re.findall('..', '%012x' % uuid.getnode()))
        info['processor'] = platform.processor()
        info['ram'] = str(round(psutil.virtual_memory().total / (1024.0 **3)))+" GB"
        # Boot Time
        boot_time_timestamp = psutil.boot_time()
        bt = datetime.fromtimestamp(boot_time_timestamp)
        info['boot-time'] = f"{bt.year}/{bt.month}/{bt.day} {bt.hour}:{bt.minute}:{bt.second}"
        
        # CPU Info
        cpu = {}
        # number of cores
        cpu['physical_cores'] = psutil.cpu_count(logical=False)
        cpu['total_cores'] = psutil.cpu_count(logical=True)
        # CPU frequencies
        cpufreq = psutil.cpu_freq()
        cpu['max _frequency'] = cpufreq.max
        cpu['min_frequency'] = cpufreq.min
        cpu['current_frequency'] = cpufreq.current
        # CPU usage
        # CPU Usage Per Core
        cores = {}
        for i, percentage in enumerate(psutil.cpu_percent(percpu=True, interval=1)):
            cores[f'core {i}'] = percentage
        cpu['cores'] = cores
        cpu['totalcpu_usage'] = str(psutil.cpu_percent())
        info['cpu'] = cpu
        
        # Memory Usage
        memory = {}
        # memory ram
        svmem = psutil.virtual_memory()
        memory['total'] =  get_size(svmem.total)
        # print('total_memory: ' + get_size(svmem.total))
        memory['available'] = get_size(svmem.available)
        memory['used'] = get_size(svmem.used)
        memory['percentage'] = svmem.percent
        # swap
        swap = psutil.swap_memory()
        memory['swap_total'] = get_size(swap.total)
        memory['swap_free'] = get_size(swap.free)
        memory['swap_used'] = get_size(swap.used)
        memory['swap_percentage'] = swap.percent
        info['memory'] = memory
        
        # Disk Usage
        disk_usage = {}
        # partitions
        partitions = psutil.disk_partitions()
        partitions_list = []
        for partition in partitions:
            partition_info = {}
            partition_info['device'] = partition.device
            partition_info['mountpoint'] = partition.mountpoint
            partition_info['file_system_type'] = partition.fstype
            try:
                partition_usage = psutil.disk_usage(partition.mountpoint)
            except PermissionError:
                # this can be caught due to the disk that isn't ready
                continue
            partition_info['total_size'] = get_size(partition_usage.total)
            partition_info['partition_used'] = get_size(partition_usage.used)
            partition_info['free'] = get_size(partition_usage.free)
            partition_info['percentage'] = partition_usage.percent
            partitions_list.append(partition_info)
        disk_usage['partitions'] = partitions_list
        # get IO statistics since boot
        disk_io = psutil.disk_io_counters()
        disk_usage['total_read'] = get_size(disk_io.read_bytes)
        # print('total_read: ' + str(get_size(disk_io.read_bytes)))
        disk_usage['total_write'] = get_size(disk_io.write_bytes)
        info['disk_usage'] = disk_usage

        # Network information
        network = {}
        if_addrs = psutil.net_if_addrs()
        interfaces = []
        for interface_name, interface_addresses in if_addrs.items():
            for address in interface_addresses:
                interface = {}
                if str(address.family) == 'AddressFamily.AF_INET':
                    interface['name'] = interface_name
                    interface['ip_address'] = address.address
                    interface['netmask'] = address.netmask
                    interface['broadcast_ip'] = address.broadcast
                elif str(address.family) == 'AddressFamily.AF_PACKET':
                    interface['name'] = interface_name
                    interface['mac_address'] = address.address
                    interface['netmask'] = address.netmask
                    interface['broadcast_mac'] = address.broadcast
                else:
                    # print(interface_name) # needs to be logged
                    # print(address) # need to be logged
                    pass
                if 'veth' not in interface_name and interface:
                    interfaces.append(interface)
        # get IO statistics since boot
        network['interfaces'] = interfaces
        net_io = psutil.net_io_counters()
        network['total_bytes_Sent'] = get_size(net_io.bytes_sent)
        network['total_bytes_received'] = get_size(net_io.bytes_recv)
        info['network'] = network
        # GPU information
        gpus = GPUtil.getGPUs()
        gpus_data = []
        if gpus != None or gpus != 'Null' :
            for gpu in gpus:
                # get the GPU id
                gpu_id = gpu.id
                # name of GPU
                gpu_name = gpu.name
                # get % percentage of GPU usage of that GPU
                gpu_load = f"{gpu.load*100}%"
                # get free memory in MB format
                gpu_free_memory = f"{gpu.memoryFree}MB"
                # get used memory
                gpu_used_memory = f"{gpu.memoryUsed}MB"
                # get total memory
                gpu_total_memory = f"{gpu.memoryTotal}MB"
                # get GPU temperature in Celsius
                gpu_temperature = f"{gpu.temperature} °C"
                gpu_uuid = gpu.uuid
                
                data={
                    'id':gpu_id,
                    'name':gpu_name,
                    'load':gpu_load,
                    'free_memory':gpu_free_memory,
                    'used_memory':gpu_used_memory,
                    'total_memory':gpu_total_memory,
                    'temperature':gpu_temperature,
                    'uuid':gpu_uuid
                    }
                gpus_data.append(data)
        info['gpus']=gpus_data
        # print("="*40 + " System Info " + "="*40)
        # print(json.dumps(info, indent=2))
        # print("="*40 + " System Info END " + "="*40)
        return json.dumps(info)
    except Exception as e:
        logging.exception(e)
Ejemplo n.º 47
0
        if systeme == 'Windows':
            uptime = "pas de valeur"
        elif systeme == 'Linux':
            # calculate the uptime
            uptime_file = open('/proc/uptime')
            uptime = uptime_file.readline().split()[0]
            uptime_file.close()
            uptime = float(uptime)
            (uptime, secs) = (int(uptime / 60), uptime % 60)
            (uptime, mins) = divmod(uptime, 60)
            (days, hours) = divmod(uptime, 24)
            uptime = 'up %d jour%s, %d:%02d' % (days, days != 1 and 's'
                                                or '', hours, mins)

        tabDisk = []
        listdisk = psutil.disk_partitions()
        for disk in listdisk:
            if (disk.fstype != 'squashfs'):
                detaildisk = psutil.disk_usage(disk.mountpoint)
                tabDisk.append({
                    'fileSystem': disk.device,
                    'size': detaildisk.total,
                    'used': detaildisk.used,
                    'available': detaildisk.free,
                    'pourcentage': detaildisk.percent,
                    'mounted': disk.mountpoint
                })
        with open('/home/charly/Bureau/Python-master/test.txt', 'r') as f:
            print(f)
            service = f.read()
            print(service)
Ejemplo n.º 48
0
 def get_variants(self):
     return [x.device for x in psutil.disk_partitions()] + ['total']
Ejemplo n.º 49
0
    def test_disk_partitions(self):
        # all = False
        ls = psutil.disk_partitions(all=False)
        # on travis we get:
        #     self.assertEqual(p.cpu_affinity(), [n])
        # AssertionError: Lists differ: [0, 1, 2, 3, 4, 5, 6, 7,... != [0]
        self.assertTrue(ls, msg=ls)
        for disk in ls:
            self.assertIsInstance(disk.device, str)
            self.assertIsInstance(disk.mountpoint, str)
            self.assertIsInstance(disk.fstype, str)
            self.assertIsInstance(disk.opts, str)
            if WINDOWS and 'cdrom' in disk.opts:
                continue
            if not POSIX:
                assert os.path.exists(disk.device), disk
            else:
                # we cannot make any assumption about this, see:
                # http://goo.gl/p9c43
                disk.device
            if SUNOS or TRAVIS:
                # on solaris apparently mount points can also be files
                assert os.path.exists(disk.mountpoint), disk
            else:
                assert os.path.isdir(disk.mountpoint), disk
            assert disk.fstype, disk

        # all = True
        ls = psutil.disk_partitions(all=True)
        self.assertTrue(ls, msg=ls)
        for disk in psutil.disk_partitions(all=True):
            if not WINDOWS:
                try:
                    os.stat(disk.mountpoint)
                except OSError as err:
                    if TRAVIS and OSX and err.errno == errno.EIO:
                        continue
                    # http://mail.python.org/pipermail/python-dev/
                    #     2012-June/120787.html
                    if err.errno not in (errno.EPERM, errno.EACCES):
                        raise
                else:
                    if SUNOS or TRAVIS:
                        # on solaris apparently mount points can also be files
                        assert os.path.exists(disk.mountpoint), disk
                    else:
                        assert os.path.isdir(disk.mountpoint), disk
            self.assertIsInstance(disk.fstype, str)
            self.assertIsInstance(disk.opts, str)

        def find_mount_point(path):
            path = os.path.abspath(path)
            while not os.path.ismount(path):
                path = os.path.dirname(path)
            return path.lower()

        mount = find_mount_point(__file__)
        mounts = [
            x.mountpoint.lower() for x in psutil.disk_partitions(all=True)
        ]
        self.assertIn(mount, mounts)
        psutil.disk_usage(mount)
Ejemplo n.º 50
0
    def get_current():
        """ Method for building an instance of NodeStatsSnapshot.
    It collects information about usage of main resource on the machine.

    Returns:
      An object of NodeStatsSnapshot with detailed explanation of resources used
      on the machine
    """
        utc_timestamp = time.mktime(datetime.now().timetuple())
        start = time.time()
        private_ip = appscale_info.get_private_ip()

        # CPU usage
        cpu_times = psutil.cpu_times()
        cpu = NodeCPU(user=cpu_times.user,
                      system=cpu_times.system,
                      idle=cpu_times.idle,
                      percent=psutil.cpu_percent(),
                      count=psutil.cpu_count())

        # AvgLoad
        loadavg = NodeLoadAvg(*os.getloadavg())

        # Memory usage
        virtual = psutil.virtual_memory()
        memory = NodeMemory(total=virtual.total,
                            available=virtual.available,
                            used=virtual.used)

        # Swap usage
        swap_mem = psutil.swap_memory()
        swap = NodeSwap(total=swap_mem.total,
                        free=swap_mem.free,
                        used=swap_mem.used)

        # Disk usage
        partitions = psutil.disk_partitions()
        partitions_dict = {}
        for part in partitions:
            usage = psutil.disk_usage(part.mountpoint)
            partitions_dict[part.mountpoint] = NodePartition(total=usage.total,
                                                             used=usage.used,
                                                             free=usage.free)
        io_counters = psutil.disk_io_counters()
        disk_io = NodeDiskIO(read_count=io_counters.read_count,
                             write_count=io_counters.write_count,
                             read_bytes=io_counters.read_bytes,
                             write_bytes=io_counters.write_bytes,
                             read_time=io_counters.read_time,
                             write_time=io_counters.write_time)

        # Network usage
        network_io = psutil.net_io_counters()
        network = NodeNetwork(bytes_sent=network_io.bytes_sent,
                              bytes_recv=network_io.bytes_recv,
                              packets_sent=network_io.packets_sent,
                              packets_recv=network_io.packets_recv,
                              errin=network_io.errin,
                              errout=network_io.errout,
                              dropin=network_io.dropin,
                              dropout=network_io.dropout,
                              connections_num=len(psutil.net_connections()))

        stats = NodeStatsSnapshot(utc_timestamp=utc_timestamp,
                                  private_ip=private_ip,
                                  cpu=cpu,
                                  memory=memory,
                                  swap=swap,
                                  disk_io=disk_io,
                                  partitions_dict=partitions_dict,
                                  network=network,
                                  loadavg=loadavg)
        logging.info("Prepared local node stats in {elapsed:.1f}s.".format(
            elapsed=time.time() - start))
        return stats
Ejemplo n.º 51
0
def disk_free():
    disk_info = dict()
    for path in [p.mountpoint for p in filter(valid_path, disk_partitions(all=True))]:
        disk_info[path] = disk_usage(path)
    return disk_info
Ejemplo n.º 52
0
def get_partitions(is_all=True):
    return psutil.disk_partitions(is_all)
Ejemplo n.º 53
0
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import psutil

disks = psutil.disk_partitions()
for i in disks:
    disk = psutil.disk_usage(i.mountpoint)
    print(r'磁盘 %s 占用空间百分比 %s' % (i.mountpoint, disk.percent))
Ejemplo n.º 54
0
def get_sys_details():
    """ Get every owns of device information and returns a dict """

    try:
        cpu_info = {}
        os_info = {}
        network_info = {}
        system_info = {}
        battery_info = {}
        disc_partition_info = {}
        partition_storage = {}
        gpu_info = nvgpu.gpu_info()[0]

        if not gpu_info:
            gpu_info = {}

        os_info['platform'] = platform.system()
        os_info['platform-release'] = platform.release()
        os_info[
            'Operating System'] = f'{platform.system()} {platform.release()}'
        os_info['platform-version'] = platform.version()
        system_info['Architecture'] = platform.machine()
        network_info['hostname'] = socket.gethostname()
        network_info['IP-address'] = socket.gethostbyname(socket.gethostname())
        system_info['mac-address'] = ':'.join(
            re.findall('..', '%012x' % uuid.getnode()))
        cpu_info['processor'] = platform.processor()
        cpu_info['Cores'] = psutil.cpu_count()
        cpu_info['Frequency - Current (MHz)'] = psutil.cpu_freq().current
        cpu_info['Frequency - Minimum (MHz)'] = psutil.cpu_freq().min
        cpu_info['Frequency - Maximum (MHz)'] = psutil.cpu_freq().max
        """ Battery Status """
        battry_obj = psutil.sensors_battery()
        battery_info['Percent'] = battry_obj.percent

        if battry_obj.secsleft != psutil.POWER_TIME_UNLIMITED and battry_obj.secsleft != psutil.POWER_TIME_UNKNOWN:
            total_seconds = battry_obj.secsleft
            hours = total_seconds // 3600
            total_seconds %= 3600
            minutes = total_seconds // 60
            battery_info[
                'Time Left'] = f'{hours} Hours, {minutes} Minutes (approx)'
        elif battry_obj.secsleft == psutil.POWER_TIME_UNLIMITED:
            battery_info['Time Left'] = 'N/A -- Plugged In'
        elif battry_obj.secsleft == psutil.POWER_TIME_UNKNOWN:
            battry_obj['Time Left'] = 'N/A -- Battery Not Available'
        else:
            battry_obj['Time Left'] = 'N/A -- Unknown'
        battery_info['Power Plugged'] = battry_obj.power_plugged
        """ Memory (RAM) """
        ram_info = bytes_to_gigs(psutil.virtual_memory())
        """ Disc """
        partitions_list = psutil.disk_partitions()
        for partitions in partitions_list:
            temp = []
            for each in partitions:
                temp.append(each)
            disc_partition_info[partitions.device] = temp
            partition_storage[partitions.device] = bytes_to_gigs(
                psutil.disk_usage(partitions.device))

        dic_list = [
            network_info, os_info, system_info, cpu_info, gpu_info,
            battery_info, ram_info, disc_partition_info, partition_storage
        ]
        dic_label = 'Network info,OS Info,System Info,CPU Info,GPU Info,Battery Info,RAM Info,Disc Info,Partition Info'.split(
            ',')

        return {label: val for label, val in zip(dic_label, dic_list)}

    except:
        pass
Ejemplo n.º 55
0
def send_node_info(channel, exchange, routing_key):
    import socket
    message = {}

    # 消息id为0
    message["id"] = 0
    # 消息的类型
    message["type"] = nsm_agent_modules.message_type["NSM_AGENT_CHECK"]
    # func id
    message["fun_id"] = nsm_agent_modules.check_fun_id[
        "NSM_AGENT_CHECK_NODE_INFO"]
    message["echo"] = False
    message["sync"] = False
    message["timestamp"] = time.time()
    message["shost"] = nsm_lib.getLocalIp()

    # fill body
    message["body"] = {}

    # get node、cpu 、mem 、disk info, use psutil module fun

    # get cpu info -----------------------------------------------------------------------------
    message["body"]["cpu"] = {}
    # try:
    #     cmd = "top -bn1 | grep \"Cpu(s)\" | sed \"s/.*, *\\([0-9.]*\\)%* id.*/\\1/\" | awk '{print 100-$1}'"
    #     result = os.popen(cmd, 'r').readline()
    #     message["body"]["cpu"]["cpu_usage"] = result
    # except:
    #     pass

    try:
        message["body"]["cpu"]["cpu_usage"] = psutil.cpu_percent(1)
    except:
        pass

    #get mem usage---------------------------------------------------------------------------
    try:
        mem = psutil.virtual_memory()
        message["body"]["cpu"]["mem_usage"] = str(mem.percent)
    except:
        pass

    #get net info-----------------------------------------------------------------------------
    message["body"]["net_card"] = []
    net_card_list = message["body"]["net_card"]
    net_card = {}

    stats = psutil.net_if_stats()
    for nic, addrs in psutil.net_if_addrs().items():
        net_card = {}
        if nic == "lo":
            continue
        else:
            net_card["name"] = nic
        if nic in stats:
            st = stats[nic]
            if st.isup:
                net_card["status"] = 1
            else:
                net_card["status"] = 0
        for addr in addrs:
            if addr.family == socket.AF_INET or addr.family == socket.AF_INET6:
                net_card["ip"] = addr.address
            elif addr.family == psutil.AF_LINK:
                net_card["mac"] = addr.address
        net_card_list.append(net_card)

    # #get net card info
    # message["body"]["net_card"] = []
    # res_list = message["body"]["net_card"]
    #
    # #get name mac ip status等
    # ifc = subprocess.Popen(["ifconfig", "-a"], stdout=subprocess.PIPE)
    # res = {}
    # ip = None
    # mac = None
    # ip_status = 0
    # mac_status = 0
    #
    # for x in ifc.stdout:
    #     if not x.strip():
    #         # 空行
    #         if not res: #如果res是空后面不用处理
    #             continue
    #         if ip:
    #             res["ip"] = ip.group(1)
    #         else:
    #             res["ip"] = ""
    #
    #         if mac:
    #             res["mac"] = mac.group(1)
    #         else:
    #             res["mac"] = ""
    #         res_list.append(res)
    #
    #         res = {}
    #         ip = None
    #         mac = None
    #         ip_status = 0
    #         mac_status = 0
    #     elif not res:
    #         # 第一行
    #         try:
    #             name = re.match(r'[A-Za-z0-9-]+', x).group()
    #             if name == "lo":
    #                 # lo 后面的行都会走这个流程
    #                 continue
    #             res["name"] = name
    #             if re.search(r'UP', x):
    #                 res["status"] = 1
    #             else:
    #                 res["status"] = 0
    #         except:
    #             #这个地方后续要处理一下
    #             pass
    #     else:
    #         # 其他行
    #         try:
    #             if ip_status == 0 and not ip:
    #                 ip = re.match(r'\s+inet\s+(\S+)', x)
    #             if ip:
    #                 ip_status = 1
    #             if mac_status == 0 and not mac:
    #                 mac = re.match(r'\s+ether\s+(\S+)', x)
    #             if mac:
    #                 mac_status = 1
    #         except:
    #             # 这个地方后续要处理一下
    #             pass

    #得到每个网卡的收发速率
    for i in range(len(net_card_list)):
        name = net_card_list[i]["name"]
        cmd = "iftop -i " + name + " -t -s 1 | grep -i \"^total\" | awk -F\":\" '{print $2}' | awk '{print $1}'"
        j = 0
        try:
            for line in os.popen(cmd).readlines():
                if j == 0:
                    net_card_list[i]["send_rate"] = str(line)
                elif j == 1:
                    net_card_list[i]["rec_rate"] = str(line)
                j += 1
        except:
            pass

    #得到磁盘信息--------------------------------------------------------------
    message["body"]["disk"] = []
    disk_table = message["body"]["disk"]
    disk = {}

    #先使用shell命令得到磁盘列表
    # cmd = "lsblk | awk '{if($1~/^.{1}d[a-z]/)print $1}'"
    cmd = "lsblk | awk '{if($1~/^[a-z].*/)print $1}'"
    disk_list = []
    for line in os.popen(cmd).readlines():
        line = line.strip('\n')
        disk_list.append(line)

    for part in psutil.disk_partitions(all=False):
        if os.name == 'nt':
            if 'cdrom' in part.opts or part.fstype == '':
                # skip cd-rom drives with no disk in it; they may raise
                # ENOENT, pop-up a Windows GUI error for a non-ready
                # partition or just hang.
                continue

        # 挂载点没有ceph盘的标识
        if not re.search("ceph", part.mountpoint):
            #已经挂载的但是非ceph使用的磁盘从磁盘列表移除
            for i in range(len(disk_list)):
                if re.search(disk_list[i], part.device):
                    disk_list.pop(i)
                    break
            continue
        '''
        disk dic example:
        disk =  {
                    "sda":{"size":314151235, "used":4534535},
                    "sdb":{"size":314151235, "used":4534535},
                    "sdc":{"size":314151235, "used":4534535}
                }
        '''
        for i in range(len(disk_list)):
            if re.search(disk_list[i], part.device):
                if not disk.has_key(disk_list[i]):
                    disk[disk_list[i]] = {}
                usage = psutil.disk_usage(part.mountpoint)

                if not disk[disk_list[i]].has_key("size"):
                    disk[disk_list[i]]["size"] = int(usage.total)
                else:
                    disk[disk_list[i]]["size"] += int(usage.total)

                if not disk[disk_list[i]].has_key("used"):
                    disk[disk_list[i]]["used"] = int(usage.used)
                else:
                    disk[disk_list[i]]["used"] += int(usage.used)

    for key in disk.keys():
        disk_info = {}
        disk_info["name"] = key
        disk_info["size"] = disk[key]["size"]
        disk_info["used"] = disk[key]["used"]
        disk_info["status"] = 1

        disk_table.append(disk_info)

    #现在cpeh使用的磁盘已经记录在disk字典里了,我们从disk_list里删除disk字典记录的磁盘
    for key in disk.keys():
        disk_list.remove(key)

    print disk_list

    #现在如果disk_list还有元素,说明是未使用的磁盘, 这个用命令取在psutil模块中不能取到没有挂载的块设备
    for disk_name in disk_list:
        cmd = "lsblk -b | grep " + disk_name + " | awk '{print $4}'"
        result = os.popen(cmd).readline()
        result.strip("\n")
        disk_info = {}
        disk_info["name"] = disk_name
        disk_info["size"] = int(result)
        disk_info["used"] = 0
        disk_info["status"] = 0
        disk_table.append(disk_info)

    #------------------------------------------------------------------------------------
    message = json.dumps(message, ensure_ascii=False)
    print message

    channel.basic_publish(exchange=exchange,
                          routing_key=routing_key,
                          body=message)
Ejemplo n.º 56
0
def run_data():
    class Spinner:
        busy = False
        delay = 0.1

        @staticmethod
        def spinning_cursor():
            while 1:
                for cursor in '|/-\\':
                    yield cursor

        def __init__(self, delay=None):
            self.spinner_generator = self.spinning_cursor()
            if delay and float(delay): self.delay = delay

        def spinner_task(self):
            while self.busy:
                sys.stdout.write(next(self.spinner_generator))
                sys.stdout.flush()
                time.sleep(self.delay)
                sys.stdout.write('\b')
                sys.stdout.flush()

        def start(self):
            self.busy = True
            threading.Thread(target=self.spinner_task).start()

        def stop(self):
            self.busy = False
            time.sleep(self.delay)

    # Get MAC address
    mac_address = (hex(uuid.getnode())).replace('L', '')
    print('your mac_address : ', mac_address)

    # Get Local IP address
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect(('8.8.8.8', 1))  # connect() for UDP doesn't send packets
    local_ip_address = s.getsockname()[0]
    print('your local_ip_address : ', local_ip_address)

    fromaddr = from_email_id
    password = from_email_pass

    email_id = to_email_id
    name_of_developer = ''

    # Connect elasticsearch DB
    machine_details_table = 'machine_details'
    process_details_table = 'process_details'
    disk_details_table = 'disk_details'

    from elasticsearch import Elasticsearch

    es = Elasticsearch([elastic_url],
                       http_auth=(elastic_username, elastic_password))
    es.indices.create(index=machine_details_table, ignore=400)
    es.indices.create(index=process_details_table, ignore=400)
    es.indices.create(index=disk_details_table, ignore=400)

    spinner = Spinner()
    print('process is going on...', end='')
    spinner.start()

    run_status = 'pending'
    timestamp = datetime.datetime.now() - timedelta(hours=5, minutes=30)
    tstamp = datetime.datetime.today().strftime('%Y:%m:%d_%H:%M:%S')
    mac_date = str(mac_address) + '_' + str(tstamp)

    # For Sending mail
    def send_mail(alert_type, args):
        import smtplib
        from email.mime.multipart import MIMEMultipart
        from email.mime.text import MIMEText
        from email.mime.base import MIMEBase
        from email import encoders

        try:
            # fromaddr = '*****@*****.**'
            toaddr = email_id.split(',')

            msg = MIMEMultipart()

            msg['From'] = fromaddr
            msg['To'] = ",".join(toaddr)
            msg['Subject'] = str(local_ip_address) + " Machine info :"

            if alert_type == 1:
                body = 'Respected Sir/Madam, \n\n In the system : ' + str(
                    local_ip_address) + ' , Used By : ' + str(
                        name_of_developer
                    ) + '.\n Available memory is only : ' + str(
                        args) + ' %  ( ' + str(hdd_available) + ' of ' + str(
                            hdd_total) + ' ) \n Please take care of it.'
            elif alert_type == 2:
                body = 'Respected Sir/Madam, \n\n In the system : ' + str(
                    local_ip_address
                ) + ' , Used By : ' + str(name_of_developer) + '.\n  : ' + str(
                    args
                ) + ' service is not running  \n Please take care of it.'
            elif alert_type == 3:
                body = 'Respected Sir/Madam, \n\n In the system : Used By ' + str(local_ip_address) + ' , Used By : ' + str(
                    name_of_developer) + '.\n : Following new processes found \n(' + str(
                    args) + ') \n Please take care of it. you can add valid process in the following sheet' \
                            '  \n:https://docs.google.com/spreadsheets/d/1iSVwJ7doRafUlfxoQ_w66fjriDOUt_Lmfwd1Oip8Fig/edit#gid=868838991'

            msg.attach(MIMEText(body, 'plain'))

            p = MIMEBase('application', 'octet-stream')
            s = smtplib.SMTP('smtp.gmail.com', 587)
            s.starttls()
            s.login(fromaddr, password)
            text = msg.as_string()
            # sending the mail
            s.sendmail(fromaddr, toaddr, text)
            # terminating the session
            s.quit()
            print('Email  sent')
        except Exception as e:
            print('Error in sending Mail : ' + str(e))

    drive_wise_hdd, cpu_name, os_version, system_name = '', '', '', ''
    current_ram_usage = available_disk_percentage = ram = hdd_available = hdd_total = cpu_utilization = no_of_prod_exe = no_of_unprod_exe = 0
    mysql_running = False
    # os_architecture
    try:
        os_architecture = platform.architecture()[0]
    except:
        os_architecture = ''
    # os
    try:
        operating_system = platform.system()
    except:
        operating_system = ''

    if run_status == 'pending':
        # Get CPU utilization
        try:
            os_version = platform.uname().release
            cpu_utilization = psutil.cpu_percent()
        except Exception as e:
            os_version = ''
            cpu_utilization = 0

        # current_ram_usage
        try:
            current_ram_usage = psutil.virtual_memory()[2]
        except:
            current_ram_usage = -1

        # MySQL service status
        mysql_running = False
        try:
            if psutil.win_service_get('MySQL56').as_dict(
            )['status'] == 'running' or psutil.win_service_get(
                    'MySQL80').as_dict()['status'] == 'running':
                mysql_running = True
        except Exception as e:
            try:
                if psutil.win_service_get(
                        'MySQL80').as_dict()['status'] == 'running':
                    mysql_running = True
            except Exception as e:
                print(e)

        if mysql_running == False:
            send_mail(2, 'mysqld')
        # Get running Process
        try:
            system_name = platform.node()

            un_recognized_process = []
            p_df = pd.read_csv(
                'https://docs.google.com/spreadsheets/d/1iSVwJ7doRafUlfxoQ_w66fjriDOUt_Lmfwd1Oip8Fig/export?gid=1835197844&format=csv'
            )
            p_list = list(p_df['virus_name'])
            process_name_list = []
            prod_process_list = []
            unprod_process_list = []
            pids = psutil.pids()
            for pid in pids:
                try:
                    p = psutil.Process(pid)
                    process_name = p.name()
                    if not str(p.exe()).lower().strip().startswith('c:'):
                        prod_process_list.append(process_name)
                        process_type = 'productive'
                    else:
                        unprod_process_list.append(process_name)
                        process_type = 'unproductive'
                    process_memory = (p.memory_info()[0]) / 10**6
                    process_memory_percent = p.memory_percent()
                    process_name_list.append(process_name)

                    if process_name in p_list:
                        un_recognized_process.append(process_name)
                    p1 = {
                        'mac_address': mac_address,
                        'local_ip_address': local_ip_address,
                        'name_of_developer': name_of_developer,
                        'process_id': pid,
                        'process_name': process_name,
                        'process_memory': process_memory,
                        'process_memory_percent': process_memory_percent,
                        'process_type': process_type,
                        'run_date': timestamp
                    }

                    es.index(index=process_details_table,
                             doc_type='wmi_data',
                             body=p1)
                except Exception as e:
                    pass
            no_of_prod_exe = len(prod_process_list)
            no_of_unprod_exe = len(unprod_process_list)
            if len(un_recognized_process) > 0:
                send_mail(3, ', '.join(un_recognized_process))
        except Exception as e:
            print((e))
            system_name = ''
            no_of_prod_exe = no_of_unprod_exe = 0

        # HDD space
        try:
            drive_data = {}
            hdd_total = 0
            hdd_available = 0
            for ps in [p.device for p in psutil.disk_partitions()]:
                try:
                    drive_name = ps[0]
                    total_disk_space = int(
                        psutil.disk_usage(ps).total / (1024**3))
                    available_disk_space = int(
                        psutil.disk_usage(ps).free / (1024**3))
                    available_disk_percentage = int(
                        100 - psutil.disk_usage(ps).percent)
                    drive_data[str(drive_name)] = str(
                        available_disk_space) + '  of ' + str(total_disk_space)
                    d1 = {
                        'mac_address': mac_address,
                        'local_ip_address': local_ip_address,
                        'name_of_developer': name_of_developer,
                        'name_of_drive': drive_name,
                        'total_disk_space': total_disk_space,
                        'available_disk_space': available_disk_space,
                        'available_disk_percentage': available_disk_percentage,
                        'run_date': timestamp
                    }
                    es.index(index=disk_details_table,
                             doc_type='wmi_data',
                             body=d1)
                    hdd_available += available_disk_space
                    hdd_total += total_disk_space
                except Exception as e:
                    logging.error('In disk details insertion : ' + str(e))
            drive_wise_hdd = str(drive_data).replace('{', '').replace('}', '')
            available_disk_percentage = int(
                round((float(hdd_available) * 100) / hdd_total, 2))
            if int(available_disk_percentage) < 20:
                send_mail(1, available_disk_percentage)
        except:
            hdd_total = available_disk_percentage = hdd_available = 0

        # RAM
        ram = psutil.virtual_memory().total / (1024**3)
        run_status = 'done'
        cpu_name = platform.processor()
        system_name = socket.gethostname()

    # Machine details Db insertion
    try:
        e1 = {
            'mac_date': mac_date,
            'mac_address': mac_address,
            'os_architecture': os_architecture,
            'local_ip_address': local_ip_address,
            'operating_system': str(operating_system),
            'os_version': str(os_version),
            'name_of_developer': str(name_of_developer),
            'cpu_utilization': int(cpu_utilization),
            'system_name': str(system_name),
            'ram': int(ram),
            'current_ram_usage': int(current_ram_usage),
            'hdd_total': int(hdd_total),
            'hdd_available': int(hdd_available),
            'available_disk_percentage': int(available_disk_percentage),
            'drive_wise_hdd': str(drive_wise_hdd),
            'cpu_name': str(cpu_name),
            'run_status': str(run_status),
            'run_date': timestamp,
            'no_of_prod_exe': no_of_prod_exe,
            'no_of_unprod_exe': no_of_unprod_exe,
            'mysql_running': (mysql_running)
        }
        es.index(index=machine_details_table,
                 doc_type='wmi_data',
                 id=mac_date,
                 body=e1)
        spinner.stop()
        print('Machine details inserted')

    except Exception as e:
        logging.error(e)
Ejemplo n.º 57
0
import psutil
from pprint import pprint as pp

pp(psutil.disk_partitions())
print()
pp(psutil.disk_partitions(all))
print()

pp(psutil.disk_usage('/home'))
Ejemplo n.º 58
0
                color = BLUE
                prefix = GRAY + "mac: " + RT
            # ipv6
            elif snic.family == 10:
                color = CYAN
                prefix = GRAY + "ipv6:" + RT
            # ipv4
            elif snic.family == 2:
                color = RT
                prefix = GRAY + "ipv4:" + RT
            print(' ', prefix, color + snic.address.split('%')[0], RT)
    except:
        pass

# Disk info
print()
head = [GRAY + 'Device', 'mount', 'fs', 'use', 'used/total (GB)' + RT]
disk_table = []
for partition in sorted(psutil.disk_partitions()):
    space = psutil.disk_usage(partition.mountpoint)
    used = str(round(space.used / (1024 * 1024 * 1024), 2))
    total = str(round(space.total / (1024 * 1024 * 1024), 2))
    percent = str(round(space.percent)) + "%"
    disk_table.append([
        GREEN + partition.device + RT, partition.mountpoint, partition.fstype,
        CYAN + percent + RT, BLUE + used + RT + "/" + BLUE + total + RT
    ])

print(tabulate.tabulate(disk_table, headers=head, tablefmt="plain"))
print()
Ejemplo n.º 59
0
        devInfo = decode_device_info(deviceRef)
        print("devInfo", devInfo)

checkDevice = devicesConnected[0]
#checkDevice = devicesConnected[1]
#checkDevice = devicesConnected[3]

for att in checkDevice:
    print("{0} = {1}", att, checkDevice[att])

diskInfo = get_disk_info(checkDevice)
print("diskInfo", diskInfo)

partitions = get_partitions_from_disk(checkDevice)
print("partitions", partitions)
checkPartition = partitions[0]
print("")
print(checkPartition)
for att in checkPartition:
    print("{0} = {1}", att, checkPartition[att])
print("")
for p in psutil.disk_partitions():
    if p.device in checkPartition.device_node:
        print("  {}: {}".format(p.device, p.mountpoint))

print("mount partition", checkPartition.device_node)
mountPoint = checkPartition.device_node
mountPoint = "dev/sda1"
#checkPartition.device_node
mount(mountPoint, '/mnt/pdisk', 'vfat', 'rw')
#umount('/mnt/pdisk')
Ejemplo n.º 60
0
print '获取空闲的内存信息:'
print mem.free
print ''

print '获取SWAP分区信息:'
print psutil.swap_memory()
print ''

print '===============获取磁盘信息=============='

print '获取磁盘IO信息:'
print psutil.disk_io_counters()
print ''

print '获取磁盘的完整信息:'
print psutil.disk_partitions()
print ''

print '获取分区表的参数:'
print psutil.disk_usage('/')
print ''

print '获取单个分区IO个数:'
print psutil.disk_io_counters(perdisk=True)#perdisk=True 参数获取单个分区IO个数
print ''

print '===============获取网络信息=============='