Esempio n. 1
0
 def renderProject(p):
     html = '<div class="block_data">'
     if p.isActive():
         html += '<img alt="" src="/dashboard/static/icons/radar.png">'
         html += '<a href="#" onclick="goTo(\'#%s\')">%s</a>' % (p.id,
                                                                 p.name)
         html += '<span class="block_data_attr">active</span>'
     else:
         html += '<img alt="" src="/dashboard/static/icons/radar-grey.png">'
         html += '<a href="#" onclick="goTo(\'#%s\')">%s</a>' % (p.id,
                                                                 p.name)
         html += '<span class="block_data_attr">inactive</span>'
     html += '<span class="block_data_attr"><b>scanners</b> %s</span>' % formatNumber(
         len(p.locations))
     if p.start:
         html += '<span class="block_data_attr"><b>start</b> %s</span>' % getRelativeTime(
             p.start, wrapper=htmlSpanWrapper, levels=2)
     if p.end:
         html += '<span class="block_data_attr"><b>end</b> %s</span>' % getRelativeTime(
             p.end, wrapper=htmlSpanWrapper, levels=2)
     if len(p.disabled_plugins) > 0:
         html += '<span class="block_data_attr"><b>disabled</b> %s</span>' % ', '.join(
             sorted(p.disabled_plugins))
     html += '</div>'
     return html
Esempio n. 2
0
 def renderUptime():
     html = '<div class="block_data"><img alt="" src="/dashboard/static/icons/clock-arrow.png">Uptime'
     if 'made' in self.conn_time:
         html += '<span class="block_data_attr"><b>connection</b> %s</span>' % getRelativeTime(
             int(float(self.conn_time['made'])),
             pastSuffix="",
             wrapper=htmlSpanWrapper)
     if self.gyrid_uptime != None and self.gyrid_connected == True:
         html += '<span class="block_data_attr"><b>gyrid</b> %s</span>' % getRelativeTime(
             self.gyrid_uptime, pastSuffix="", wrapper=htmlSpanWrapper)
     if self.host_uptime != None:
         html += '<span class="block_data_attr"><b>system</b> %s</span>' % getRelativeTime(
             self.host_uptime, pastSuffix="", wrapper=htmlSpanWrapper)
     html += '</div>'
     return html
Esempio n. 3
0
    def render(self):
        """
        Render this sensor to HTML.

        @return   (str)   HTML representation of this sensor.
        """
        html = '<div class="block_data">'
        vendor = macvendor.getVendor(self.mac)
        mac = self.mac.upper(
        ) if vendor == None else '<span title="%s">%s</span>' % (
            vendor, self.mac.upper())
        try:
            detc = [self.lag[i][1] for i in sorted(self.lag.keys()) if i <= 15]
            udetc = [
                self.lag[i][2] for i in sorted(self.lag.keys()) if i <= 15
            ]
        except IndexError:
            detc = udetc = []

        if self.connected == False:
            html += '<img alt="" src="/dashboard/static/icons/%s-grey.png">%s' % (
                self.hwType, mac)
            if self.disconnect_time != None:
                html += '<span class="block_data_attr"><b>disconnected</b> %s</span>' % getRelativeTime(
                    int(float(self.disconnect_time)), wrapper=htmlSpanWrapper)
        else:
            html += '<img alt="" src="/dashboard/static/icons/%s.png">' % self.hwType
            if self.last_rotation != None:
                if time.time() - self.last_rotation < 300:
                    img = 'rotation'
                else:
                    img = 'rotation-grey'
                html += '<img alt="" src="/dashboard/static/icons/%s.png">' % img
            html += mac
            if self.last_activity != None:
                html += '<span class="block_data_attr"><b>last activity</b> %s</span>' % getRelativeTime(
                    int(float(self.last_activity)), wrapper=htmlSpanWrapper)
        if self.last_data != None:
            html += '<span class="block_data_attr"><b>last data</b> %s</span>' % getRelativeTime(
                int(float(self.last_data)), wrapper=htmlSpanWrapper)
        if len([i for i in detc if i > 0]) > 0:
            html += '<span class="block_data_attr"><b>detections</b> ' + \
                '<span title="1, 5 and 15 minutes moving averages">%s</span></span>' % \
                ', '.join([formatNumber(i) for i in detc])
            html += '<span class="block_data_attr"><b>unique</b> ' + \
                '<span title="1, 5 and 15 minutes moving averages">%s</span></span>' % \
                ', '.join([formatNumber(i) for i in udetc])
            html += '<span class="block_data_attr"><b>total</b> %s</span>' % formatNumber(
                self.detections)
        elif self.detections > 0:
            html += '<span class="block_data_attr"><b>detections</b> %s</span>' % formatNumber(
                self.detections)
        html += '</div>'
        return html
Esempio n. 4
0
    def getMessageBody(self, level):
        """
        Get the message body for this alert, given the level.

        @param    level (Alert.Level)   The level to check.
        @return   (str)                 The corresponding message body.
        """
        msg = Alert.Level.String[level]
        msg += ' - %s: \r\n\r\n' % getRelativeTime(self.etime)
        msg += Alert.Type.Message[self.type] % {'origin': self.origin,
                                                'module': self.module}
        msg += '\r\n\r\n'
        if self.message:
            msg += self.message.strip()
            msg += '\r\n\r\n'
        msg += '--\r\nThis event occurred at %s.' % \
            time.strftime("%Y%m%d-%H%M%S-%Z", time.localtime(self.etime))
        return msg
Esempio n. 5
0
 def renderBalance():
     if 'data' in self.mv_balance:
         try:
             mb = self.mv_balance['data'] / 1024.0 / 1024.0
         except:
             return ''
         if mb <= 200:
             html = '<div class="block_data"><img alt="" src="/dashboard/static/icons/shield-red.png">SIM balance'
         elif mb <= 500:
             html = '<div class="block_data"><img alt="" src="/dashboard/static/icons/shield-yellow.png">SIM balance'
         elif 'is_expired' in self.mv_balance and self.mv_balance[
                 'is_expired']:
             html = '<div class="block_data"><img alt="" src="/dashboard/static/icons/shield-red.png">SIM balance'
         elif 'valid_until' in self.mv_balance and not self.mv_balance[
                 'is_expired']:
             if int(time.strftime('%s', time.strptime(self.mv_balance['valid_until'], '%Y-%m-%d %H:%M:%S'))) - \
                 int(time.time()) <= 60*60*24*7:
                 html = '<div class="block_data"><img alt="" src="/dashboard/static/icons/shield-yellow.png">SIM balance'
             else:
                 return ''
         else:
             return ''
         html += '<span class="block_data_attr"><b>data</b> %s MB</span>' % formatNumber(
             mb)
         if 'is_expired' in self.mv_balance and self.mv_balance[
                 'is_expired']:
             html += '<span class="block_data_attr"><b>expired</b> %s</span>' % (
                 'yes' if self.mv_balance['is_expired'] else 'no')
         if 'valid_until' in self.mv_balance and not self.mv_balance[
                 'is_expired']:
             html += '<span class="block_data_attr"><b>expires</b> %s</span>' % \
                 getRelativeTime(float(time.strftime('%s', time.strptime(self.mv_balance['valid_until'],
                 '%Y-%m-%d %H:%M:%S'))), wrapper=htmlSpanWrapper)
         if self.mv_updated:
             html += '<span class="block_data_attr"><b>updated</b> %s</span>' % getRelativeTime(
                 self.mv_updated, wrapper=htmlSpanWrapper)
         html += '</div>'
         return html
     else:
         return ''
Esempio n. 6
0
        def renderLag():
            lag = []
            tlag = {}
            for sens in self.sensors.values():
                lag.extend([(sens.lag[i][0] / sens.lag[i][1])
                            for i in sorted(sens.lag.keys())
                            if (i <= 15 and sens.lag[i][1] > 0)])
                tlag.update(sens.lag)

            if len([i for i in lag[1:] if i >= 5]) > 0:
                provider = self.ip_provider.get(
                    list(self.connections)[0][0], (None, None))[0]
                html = '<div class="block_data"><img alt="" src="/dashboard/static/icons/network-cloud.png">Network'
                l = []
                for i in sorted(tlag.keys()):
                    if i <= 15:
                        if tlag[i][1] == 0:
                            l.append('nd')
                        else:
                            avg = tlag[i][0] / tlag[i][1]
                            if avg < 60:
                                l.append(
                                    formatNumber(float("%0.2f" % (avg))) + 's')
                            else:
                                l.append(
                                    getRelativeTime(time.time() - avg,
                                                    pastSuffix=""))
                if len([i for i in l if i != 'nd']) > 0:
                    html += '<span class="block_data_attr"><b>lag</b> ' + \
                        '<span title="1, 5 and 15 minutes moving averages">%s</span></span>' %  ', '.join(l)
                html += '<span class="block_data_attr"><b>ip</b> %s</span>' % list(
                    self.connections)[0][0]
                if provider:
                    html += '<span class="block_data_attr"><b>provider</b> %s</span>' % provider
                html += '</div>'
                return html
            else:
                return ''
Esempio n. 7
0
    def getStatus(self):
        """
        Return the current status of the Move plugin and cache. For use in the status plugin.
        """
        r = []

        if self.conn == None:
            r.append({'status': 'error'})
            r.append({'id': 'error', 'str': 'API details missing'})
            return r

        m = self.conn.measureCount
        cache = sum(len(self.measurements[i]) for i in self.measurements)
        now = time.time()

        if self.config.getValue(
                'upload_enabled') == False or self.config.getValue(
                    'caching_enabled') == False:
            r.append({'status': 'disabled'})
            if self.config.getValue('upload_enabled') == False:
                r.append({'id': 'upload', 'str': 'disabled'})
            if self.config.getValue('caching_enabled') == False:
                r.append({'id': 'caching', 'str': 'disabled'})

        elif cache > 0 and (now -
                            m['last_upload']) > (self.conn.uploadInterval * 5):
            r.append({'status': 'error'})
        elif self.conn.lastError != None:
            r.append({'status': 'error'})
        else:
            r.append({'status': 'ok'})

        if self.conn.lastError != None:
            r.append({'id': 'error', 'str': self.conn.lastError.lower()})

        if m['last_upload'] > 0:
            r.append({'id': 'last upload', 'time': m['last_upload']})
        elif m['last_upload'] < 0 and self.conn.lastError == None:
            r.append({'id': 'no upload'})

        tU = m['uploads'] + m['failed_uploads']
        if tU > 0:
            r.append({
                'id':
                '<span title="%i out of %i uploads failed">hitrate</span>' %
                (m['failed_uploads'], tU),
                'str':
                '%0.2f %%' % (((m['uploads'] * 1.0) / tU) * 100)
            })

        firstData = None
        if cache <= (self.config.getValue('max_request_size') /
                     4):  # Too CPU intensive for big cache.
            firstData = time.localtime()
            for s in self.measurements.values():
                for l in s:
                    t = time.strptime(l[:15] + l[19:l.find(',')],
                                      "%Y%m%d-%H%M%S-%Z")
                    firstData = min(t, firstData)

        if cache > 0:
            if firstData != None:
                r.append({
                    'id':
                    '<span title="Data since: %s – %s">cached</span>' %
                    (time.strftime("%a %Y-%m-%d %H:%M:%S", firstData),
                     getRelativeTime(int(time.strftime("%s", firstData)))),
                    'int':
                    cache
                })
            else:
                r.append({'id': 'cached', 'int': cache})

        if self.conn.lastError == None and self.config.getValue(
                'upload_enabled') == True:
            if m['uploads'] > 0:
                r.append({
                    'id':
                    '<span title="Average upload size; total number of uploads">average upload</span>',
                    'int': (m['uploaded'] / m['uploads'])
                })

            if len(m['recent_uploads']) > 0:
                r.append(
                    {'id': '<span title="Average upload size; %i most recent uploads">recent average upload</span>' % \
                                self.maxRecent,
                     'int': (sum(m['recent_uploads']) / len(m['recent_uploads']))})

        return r
Esempio n. 8
0
        def renderNotconnected(disconnectTime, pastSuffix=""):
            html = '<div class="block_data"><img alt="" src="/dashboard/static/icons/traffic-cone.png">' + \
                'No connection%s' % pastSuffix
            if disconnectTime != None:
                html += '<span class="block_data_attr"><b>disconnected</b> %s</span>' % getRelativeTime(
                    int(float(disconnectTime)), wrapper=htmlSpanWrapper)
            elif pastSuffix == "" and self.lastConnected != None:
                html += '<span class="block_data_attr"><b>last connected</b> %s</span>' % getRelativeTime(
                    int(float(self.lastConnected)), wrapper=htmlSpanWrapper)

            html += '</div>'
            return html
Esempio n. 9
0
    def renderServer(self):
        """
        Render the server HTML block.

        @return   (str)   HTML representation of the server.
        """
        html = '<div id="server_block"><div class="block_title"><h3>Server</h3></div>'
        html += '<div class="block_topright_server">%s<img alt="" src="/dashboard/static/icons/clock-arrow.png"></div>' % \
            getRelativeTime(self.plugin.server.server_uptime, pastSuffix="", wrapper=htmlSpanWrapper)
        html += '<div style="clear: both;"></div>'
        html += '<div class="block_content">'

        if self.plugin.connectionLagProcessing == False or \
            self.plugin.config.getValue('connection_lag_processing') == False:
            html += '<div class="block_data">'
            html += '<img alt="" src="/dashboard/static/icons/warning.png">Warning'
            html += '<span class="block_data_attr">connection lag calculation disabled</span>'
            html += '</div>'

        plugins = [
            p for p in self.plugin.server.pluginmgr.getPlugins()
            if p.name != None
        ]

        # Resources
        if len(plugins) < 1 or ((len(self.plugin.load) > 0 and len([
                i for i in self.plugin.load[1:] if float(i) >=
            (self.plugin.cpuCount * 0.8)
        ]) > 0) or int(self.plugin.memfree_mb) <= 256
                                or self.plugin.diskfree_mb <= 2048):
            html += '<div class="block_data">'
            html += '<img alt="" src="/dashboard/static/icons/system-monitor.png">Resources'
            html += '<span class="block_data_attr"><b>load</b> ' + \
                '<span title="1, 5 and 15 minutes moving averages">%s</span></span>' % ', '.join(self.plugin.load)
            html += '<span class="block_data_attr"><b>ram free</b> %s</span>' % (
                formatNumber(self.plugin.memfree_mb) + ' MB')
            html += '<span class="block_data_attr"><b>disk free</b> %s</span>' % (
                formatNumber(self.plugin.diskfree_mb) + ' MB')
            html += '</div>'

        # Plugins
        for p in plugins:
            html += '<div class="block_data">'
            st = p.getStatus()
            if 'status' in st[0] and st[0]['status'] == 'error':
                html += '<img alt="" src="/dashboard/static/icons/puzzle-red.png">%s' % p.name
            elif 'status' in st[0] and st[0]['status'] == 'disabled':
                html += '<img alt="" src="/dashboard/static/icons/puzzle-grey.png">%s' % p.name
            else:
                html += '<img alt="" src="/dashboard/static/icons/puzzle.png">%s' % p.name
            for i in st:
                if len(i) == 1 and 'id' in i:
                    html += '<span class="block_data_attr">%s</span>' % i['id']
                elif len(i) > 1 and 'time' in i:
                    html += '<span class="block_data_attr"><b>%s</b> %s</span>' % (
                        i['id'],
                        getRelativeTime(i['time'], wrapper=htmlSpanWrapper))
                elif len(i) > 1 and 'str' in i:
                    html += '<span class="block_data_attr"><b>%s</b> %s</span>' % (
                        i['id'], i['str'])
                elif len(i) > 1 and 'int' in i:
                    html += '<span class="block_data_attr"><b>%s</b> %s</span>' % (
                        i['id'], formatNumber(i['int']))
            html += '</div>'

        html += '</div></div>'
        return html
Esempio n. 10
0
    def renderProject(self, project):
        """
        Render a specific project to HTML.

        @param    project (olof.datatypes.Project)   Project to render.
        @return   (str)                              HTML representation of the project.
        """
        html = '<div class="h2-outline" id="%s"><h2 onclick="goTo(\'#server_block\')">%s</h2>' % \
            (project.id, project.name)
        html += '<div class="block_content"><div class="block_data">'

        if project.isActive():
            html += '<img alt="" src="/dashboard/static/icons/radar.png">Active'
        else:
            html += '<img alt="" src="/dashboard/static/icons/radar-grey.png">Inactive'
        if project.start:
            html += '<span class="block_data_attr"><b>start</b> %s</span>' % getRelativeTime(
                project.start, wrapper=htmlSpanWrapper, levels=2)
        if project.end:
            html += '<span class="block_data_attr"><b>end</b> %s</span>' % getRelativeTime(
                project.end, wrapper=htmlSpanWrapper, levels=2)
        html += '</div>'

        if len(project.disabled_plugins) > 0:
            html += '<div class="block_data"><img alt="" src="/dashboard/static/icons/puzzle-grey.png">Disabled plugins'
            html += '<span class="block_data_attr">%s</span>' % ', '.join(
                sorted(project.disabled_plugins))
            html += '</div>'

        if len(project.locations) == 0:
            html += '</div></div>'
            return html

        scanner_status = {
            ScannerStatus.Good: 0,
            ScannerStatus.Bad: 0,
            ScannerStatus.Ugly: 0
        }
        for location in project.locations.values():
            s = self.plugin.match(location)
            scanner_status[s.getStatus()] += 1

        if len(project.locations) >= 8 and (scanner_status[ScannerStatus.Bad] > 0 or \
            scanner_status[ScannerStatus.Ugly] > 0):
            html += '<div class="block_data"><img alt="" src="/dashboard/static/icons/traffic-light-single.png">Scanner status'
            html += '<span class="block_data_attr"><b>total</b> %s</span>' % formatNumber(
                len(project.locations))
            if scanner_status[ScannerStatus.Good] > 0:
                html += '<span class="block_data_attr"><b>online</b> %s – %i%%</span>' % (
                    formatNumber(scanner_status[ScannerStatus.Good]),
                    scanner_status[ScannerStatus.Good] * 100 /
                    len(project.locations))
            if scanner_status[ScannerStatus.Bad] > 0:
                html += '<span class="block_data_attr"><b>offline</b> %s – %i%%</span>' % (
                    formatNumber(scanner_status[ScannerStatus.Bad]),
                    scanner_status[ScannerStatus.Bad] * 100 /
                    len(project.locations))
            if scanner_status[ScannerStatus.Ugly] > 0:
                html += '<span class="block_data_attr"><b>attention</b> %s – %i%%</span>' % (
                    formatNumber(scanner_status[ScannerStatus.Ugly]),
                    scanner_status[ScannerStatus.Ugly] * 100 /
                    len(project.locations))
            html += '</div>'

        html += '</div></div>'

        html += '<div class="navigation_block">'
        for location in sorted(project.locations.keys()):
            html += self.plugin.match(
                project.locations[location]).renderNavigation(project.id)
        html += '</div>'

        for location in sorted(project.locations.keys()):
            html += self.plugin.match(project.locations[location]).render(
                project.id)

        return html