Esempio n. 1
0
 def _get_net_stats():
     ifs = {}
     nets = gtop.netlist()
     for net in nets:
         netload = gtop.netload(net)
         ifs[net] = [netload.bytes_in, netload.bytes_out]
     nets.insert(0, "Net")
     return ifs, nets
Esempio n. 2
0
    def get_theme_properties(self):
        properties = {}

        def convert_bytes(bytes):
            bytes = float(bytes)
            if bytes >= 1099511627776:
                terabytes = bytes / 1099511627776
                size = '%.2fT' % terabytes
            elif bytes >= 1073741824:
                gigabytes = bytes / 1073741824
                size = '%.2fG' % gigabytes
            elif bytes >= 1048576:
                megabytes = bytes / 1048576
                size = '%.2fM' % megabytes
            elif bytes >= 1024:
                kilobytes = bytes / 1024
                size = '%.2fK' % kilobytes
            else:
                size = '%.2fb' % bytes
            return size

        # Split vnstat data into array
        def get_traffic_data(dataType, dataValue, vn):
            line = ''
            for item in vn.split("\n"):
                if "%s;%d;" % (dataType, dataValue) in item:
                    line = item.strip().split(';')
                    break
            return line

        # convert MiB and KiB into KB
        def cb(mib, kib):
            return (int(mib) * 1000000) + (int(kib) * 1000)

        """
        Get the details to display and place them as properties which are passed to
        the theme
        """

        if self.use_vnstat is False:
            bootup = datetime.datetime.fromtimestamp(
                int(gtop.uptime().boot_time)).strftime('%d.%m.%y %H:%M')
            sd = gtop.netload(self.networkdevice)
            properties["sdn"] = "DL: " + convert_bytes(sd.bytes_in)
            properties["sup"] = "UL: " + convert_bytes(sd.bytes_out)
            properties["des1"] = "Traffic since: " + bootup
            properties["title"] = self.networkdevice + " Traffic"

        else:
            vnstat, vn = g15os.get_command_output('vnstat -i ' +
                                                  self.networkdevice +
                                                  ' --dumpdb')
            if vnstat != 0:
                properties["message"] = "vnstat is not installed!"
            else:
                chErr = str(vn.find("Error"))
                if chErr != "-1":
                    properties[
                        "message"] = "No stats for device " + self.networkdevice
                else:
                    properties["title"] = self.networkdevice + " Traffic (U/D)"

                    def get_data(kind, period):
                        # get vnstat data as array, array content: 2 = unixtime, 4 = up MiB, 6 = up KiB, 3 = dn MiB, 5 = dn KiB
                        line = get_traffic_data(kind, period, vn)
                        if line[7] == '1':
                            up = convert_bytes(cb(line[4], line[6]))
                            dn = convert_bytes(cb(line[3], line[5]))
                            des = int(line[2])
                            return [up, dn, des]
                        else:
                            return None

                    if self.loadpage == 'vnstat_daily':
                        k = "d"
                        fmt = '%A'
                    elif self.loadpage == 'vnstat_monthly':
                        k = "m"
                        fmt = '%B'

                    for p in range(0, 3):
                        data = get_data(k, p)
                        if data is not None:
                            properties["d"] = "/"
                            properties["dup" + str(p + 1)] = data[0]
                            properties["ddn" + str(p + 1)] = data[1]
                            properties[
                                "des" +
                                str(p + 1)] = datetime.datetime.fromtimestamp(
                                    data[2]).strftime(fmt)

        return properties