Example #1
0
    def save(self):
        cmds = []
        cmds.append("services sp auto-config interface rules add \"%s\"" % self.name)
        cmds.append("services sp auto-config interface rules edit \"%s\" description set \"%s\"" % (self.name, self.description))
        cmds.append("services sp auto-config interface rules edit \"%s\" precedence set %s" % (self.name, self.precedence))

        if self.action_type:
            cmds.append("services sp auto-config interface rules edit \"%s\" action type enable" % self.name)
            cmds.append("services sp auto-config interface rules edit \"%s\" type set %s" % (self.name, self.action_set_type))
        else:
            cmds.append("services sp auto-config interface rules edit \"%s\" action type disable" % self.name)

        if self.action_asns:
            cmds.append("services sp auto-config interface rules edit \"%s\" action asns enable" % self.name)
            cmds.append("services sp auto-config interface rules edit \"%s\" peers set %s" % (self.name, self.action_set_asn))
        else:
            cmds.append("services sp auto-config interface rules edit \"%s\" action asns disable" % self.name)

        if self.action_set_mo:
            cmds.append("services sp auto-config interface rules edit \"%s\" action managed_objects enable" % self.name)
            cmds.append("services sp auto-config interface rules edit \"%s\" managed_objects type set %s" % (self.name, self.action_set_mo_type))
            for mo in self.action_set_mos:
                cmds.append("services sp auto-config interface rules edit \"%s\" managed_objects add \"%s\"" % (self.name, mo))
        else:
            cmds.append("services sp auto-config interface rules edit \"%s\" managed_objects clear" % self.name)

        cmds.append("services sp auto-config interface rules edit \"%s\" regexp set \"%s\"" % (self.name, self.match_intf_desc_regex))

        pf = PeakflowSOAP(self.co)
        for cmd in cmds:
            res = pf.cliRun(cmd)
        return res
Example #2
0
 def from_peakflow(cls, co):
     """
     """
     pf = PeakflowSOAP(co)
     config = pf.cliRun("config show")
     mos = cls.from_conf(config["results"])
     for mo in mos:
         mo.co = co
     return mos
Example #3
0
 def from_peakflow(cls, co):
     """
     """
     pf = PeakflowSOAP(co)
     config = pf.cliRun("config show")
     mos = cls.from_conf(config['results'])
     for mo in mos:
         mo.co = co
     return mos
Example #4
0
    def save(self):
        # TODO: implement support for parents..
        cmds = []
        cmds.append('services sp managed_objects add "%s"' % self.name)
        for tag in self.tags:
            cmds.append('services sp managed_objects edit "%s" tags add "%s"' % (self.name, tag))
        cmds.append('services sp managed_objects edit "%s" description set "%s"' % (self.name, self.description))
        cmds.append('services sp managed_objects edit "%s" family set "%s"' % (self.name, self.family))
        cmds.append('services sp managed_objects edit "%s" match set %s' % (self.name, self.match.get_conf()))

        pf = PeakflowSOAP(self.co)
        for cmd in cmds:
            res = pf.cliRun(cmd)
        return res
Example #5
0
    def save(self):
        # TODO: implement support for parents..
        cmds = []
        cmds.append("services sp managed_objects add \"%s\"" % self.name)
        for tag in self.tags:
            cmds.append(
                "services sp managed_objects edit \"%s\" tags add \"%s\"" %
                (self.name, tag))
        cmds.append(
            "services sp managed_objects edit \"%s\" description set \"%s\"" %
            (self.name, self.description))
        cmds.append(
            "services sp managed_objects edit \"%s\" family set \"%s\"" %
            (self.name, self.family))
        cmds.append("services sp managed_objects edit \"%s\" match set %s" %
                    (self.name, self.match.get_conf()))

        pf = PeakflowSOAP(self.co)
        for cmd in cmds:
            res = pf.cliRun(cmd)
        return res
Example #6
0
 def __init__(self):
     self.pf = PeakflowSOAP()
Example #7
0
class Report:
    def __init__(self):
        self.pf = PeakflowSOAP()

    def get_graph(self, output_filename, mo_type, mo_id, title = None, filter2 = None, filter2_binby = 1):
        query_filter = ""
        if filter2:
            query_filter = """<filter type="%s" binby="%s"/>""" % (filter2, filter2_binby)

        query = """
            <peakflow version="1.0">
                <query id="query1" type="traffic">
                    <time start_ascii="24 hours ago" end_ascii="now"/>
                    <unit type="bps"/>
                    <search limit="100" timeout="30"/>
                    <class>in</class>
                    <class>out</class>
                    <filter type="%(mo_type)s">
                        <instance value="%(mo_type_id)s"/>
                    </filter>
                    %(filter2)s
                </query>
            </peakflow>
        """ % {
                'mo_type': mo_type,
                'mo_type_id': mo_id,
                'filter2': query_filter
                }

        gc = """<?xml version="1.0" encoding="utf-8"?>
            <peakflow version="2.0">
            <graph id="graph1">
            <title>%(title)s</title>
            <ylabel>bps</ylabel>
            <width>800</width>
            <height>300</height>
            <legend>1</legend>
            </graph>
            </peakflow>
            """ % {
                    'title': title
                    }

        res = self.pf.getTrafficGraph(query, gc)

        f = open(output_filename, "w")
        f.write(res['graph'])
        f.close()


    def get_table(self, output_filename, mo_type, mo_id, title = None, filter2 = None, filter2_binby = 1):
        """
        """
        query_filter = ""
        if filter2:
            query_filter = """<filter type="%s" binby="%s"/>""" % (filter2, filter2_binby)

        query = """
            <peakflow version="1.0">
                <query id="query1" type="traffic">
                    <time start_ascii="24 hours ago" end_ascii="now"/>
                    <unit type="bps"/>
                    <search limit="100" timeout="30"/>
                    <class>in</class>
                    <class>out</class>
                    <filter type="%(mo_type)s">
                        <instance value="%(mo_type_id)s"/>
                    </filter>
                    %(filter2)s
                </query>
            </peakflow>
        """ % {
                'mo_type': mo_type,
                'mo_type_id': mo_id,
                'filter2': query_filter
                }

        return self.pf.runXmlQuery(query)
Example #8
0
    def from_id(cls, co, alert_id):
        a = Alert()

        pf = PeakflowSOAP(co)

        # get XML from peakflow
        res = pf.getDosAlertSummariesXML(alert_id)
        # XML to python
        root = objectify.fromstring(res.encode('utf-8'))

        # extract values
        a.direction = root.alert.direction
        a.type = root.alert.get('type')
        a.destination = root.alert.resource.ip
        try:
            a.attack_start = datetime.fromtimestamp(int(root.alert.duration.get('start')))
        except:
            pass
        try:
            a.attack_stop = datetime.fromtimestamp(int(root.alert.duration.get('stop')))
        except:
            pass
        a.ongoing = bool(root.alert.duration.get('ongoing'))
        a.duration = int(root.alert.duration.get('length'))
        a.target_mo = root.alert.resource.managed_object.get('name')
        a.target_mo_id = root.alert.resource.managed_object.get('gid')
        try:
            a.protocol = root.alert.protocol
        except:
            pass
        try:
            a.impact_bps = root.alert.impact.get('bps')
        except:
            pass
        try:
            a.impact_pps = root.alert.impact.get('pps')
        except:
            pass
        a.threshold = root.alert.severity.get('threshold')
        a.threshold_unit = root.alert.severity.get('unit')

        try:
            for annotation in root.alert['annotation-list'].iterchildren():
                m = re.match("TMS mitigation '([^']+)' (started|stopped)", str(annotation.content))
                if m is not None:
                    a.mitigation_name = m.group(1)
                    if m.group(2) == 'started':
                        a.mitigation_start = datetime.fromtimestamp(int(annotation.added))
                    if m.group(2) == 'stopped':
                        a.mitigation_stop = datetime.fromtimestamp(int(annotation.added))
        except:
            pass

        res = client.service.getDosAlertDetailsXML(alert_id)
        root = objectify.fromstring(res.encode('utf-8'))
        for item in root['sample-list']:
            if item.get('name') is None:
                continue

            for prefix in item.prefixes.find('prefix'):
                if prefix.get('is_dst') == "0":
                    a.sources.append(prefix.get('cidr'))

        return a
Example #9
0
    def from_id(cls, co, alert_id):
        a = Alert()

        pf = PeakflowSOAP(co)

        # get XML from peakflow
        res = pf.getDosAlertSummariesXML(alert_id)
        # XML to python
        root = objectify.fromstring(res.encode('utf-8'))

        # extract values
        a.direction = root.alert.direction
        a.type = root.alert.get('type')
        a.destination = root.alert.resource.ip
        try:
            a.attack_start = datetime.fromtimestamp(
                int(root.alert.duration.get('start')))
        except:
            pass
        try:
            a.attack_stop = datetime.fromtimestamp(
                int(root.alert.duration.get('stop')))
        except:
            pass
        a.ongoing = bool(root.alert.duration.get('ongoing'))
        a.duration = int(root.alert.duration.get('length'))
        a.target_mo = root.alert.resource.managed_object.get('name')
        a.target_mo_id = root.alert.resource.managed_object.get('gid')
        try:
            a.protocol = root.alert.protocol
        except:
            pass
        try:
            a.impact_bps = root.alert.impact.get('bps')
        except:
            pass
        try:
            a.impact_pps = root.alert.impact.get('pps')
        except:
            pass
        a.threshold = root.alert.severity.get('threshold')
        a.threshold_unit = root.alert.severity.get('unit')

        try:
            for annotation in root.alert['annotation-list'].iterchildren():
                m = re.match("TMS mitigation '([^']+)' (started|stopped)",
                             str(annotation.content))
                if m is not None:
                    a.mitigation_name = m.group(1)
                    if m.group(2) == 'started':
                        a.mitigation_start = datetime.fromtimestamp(
                            int(annotation.added))
                    if m.group(2) == 'stopped':
                        a.mitigation_stop = datetime.fromtimestamp(
                            int(annotation.added))
        except:
            pass

        res = client.service.getDosAlertDetailsXML(alert_id)
        root = objectify.fromstring(res.encode('utf-8'))
        for item in root['sample-list']:
            if item.get('name') is None:
                continue

            for prefix in item.prefixes.find('prefix'):
                if prefix.get('is_dst') == "0":
                    a.sources.append(prefix.get('cidr'))

        return a
Example #10
0
 def __init__(self):
     self.pf = PeakflowSOAP()
Example #11
0
class Report:
    def __init__(self):
        self.pf = PeakflowSOAP()

    def get_graph(self,
                  output_filename,
                  mo_type,
                  mo_id,
                  title=None,
                  filter2=None,
                  filter2_binby=1):
        query_filter = ""
        if filter2:
            query_filter = """<filter type="%s" binby="%s"/>""" % (
                filter2, filter2_binby)

        query = """
            <peakflow version="1.0">
                <query id="query1" type="traffic">
                    <time start_ascii="24 hours ago" end_ascii="now"/>
                    <unit type="bps"/>
                    <search limit="100" timeout="30"/>
                    <class>in</class>
                    <class>out</class>
                    <filter type="%(mo_type)s">
                        <instance value="%(mo_type_id)s"/>
                    </filter>
                    %(filter2)s
                </query>
            </peakflow>
        """ % {
            'mo_type': mo_type,
            'mo_type_id': mo_id,
            'filter2': query_filter
        }

        gc = """<?xml version="1.0" encoding="utf-8"?>
            <peakflow version="2.0">
            <graph id="graph1">
            <title>%(title)s</title>
            <ylabel>bps</ylabel>
            <width>800</width>
            <height>300</height>
            <legend>1</legend>
            </graph>
            </peakflow>
            """ % {
            'title': title
        }

        res = self.pf.getTrafficGraph(query, gc)

        f = open(output_filename, "w")
        f.write(res['graph'])
        f.close()

    def get_table(self,
                  output_filename,
                  mo_type,
                  mo_id,
                  title=None,
                  filter2=None,
                  filter2_binby=1):
        """
        """
        query_filter = ""
        if filter2:
            query_filter = """<filter type="%s" binby="%s"/>""" % (
                filter2, filter2_binby)

        query = """
            <peakflow version="1.0">
                <query id="query1" type="traffic">
                    <time start_ascii="24 hours ago" end_ascii="now"/>
                    <unit type="bps"/>
                    <search limit="100" timeout="30"/>
                    <class>in</class>
                    <class>out</class>
                    <filter type="%(mo_type)s">
                        <instance value="%(mo_type_id)s"/>
                    </filter>
                    %(filter2)s
                </query>
            </peakflow>
        """ % {
            'mo_type': mo_type,
            'mo_type_id': mo_id,
            'filter2': query_filter
        }

        return self.pf.runXmlQuery(query)
Example #12
0
                      "--password",
                      help="password for SOAP API connection")
    parser.add_option("--customer-id",
                      metavar="ID",
                      help="get a graph for customer with id ID")
    parser.add_option("--output-file",
                      metavar="FILE",
                      help="write output to FILE")
    parser.add_option("--mo-type", help="MO type")
    parser.add_option("--mo-id", help="MO id")
    parser.add_option("--filter", help="Filter by [nexthop]")
    parser.add_option("--graph-title", help="title of the graph")
    parser.add_option("--graph", help="fetch data and write a graph")
    parser.add_option("--table", help="fetch data and print in tabular form")
    (options, args) = parser.parse_args()

    pf = PeakflowSOAP(options.host, options.username, options.password)

    if options.graph and not options.output_file:
        print >> sys.stderr, "Please provide an output file to write the graph to with --output-file"
        sys.exit(1)

    f = Report()
    if options.graph:
        f.get_graph(options.output_file, options.mo_type, options.mo_id,
                    options.graph_title, options.filter, 1)

    if options.table:
        print f.get_table(options.output_file, options.mo_type, options.mo_id,
                          options.graph_title, options.filter, 1)