Beispiel #1
0
 def do_GET(self):
     url = urlparse.urlparse(self.path)
     if url.path == "/metrics":
         params = urlparse.parse_qs(url.query)
         if "address" not in params:
             self.send_response(400)
             self.end_headers()
             self.wfile.write("Missing 'address' from parameters")
             return
         with open(self._config_path) as f:
             config = yaml.safe_load(f)
         output = collect_snmp(config, params["address"][0])
         self.send_response(200)
         self.send_header("Content-Type", CONTENT_TYPE_LATEST)
         self.end_headers()
         self.wfile.write(output)
     elif url.path == "/":
         self.send_response(200)
         self.end_headers()
         self.wfile.write(
             """<html>
   <head><title>SNMP Exporter</title></head>
   <body>
   <h1>SNMP Exporter</h1>
   <p>Visit <code>/metrics?address=1.2.3.4</code> to use.</p>
   </body>
   </html>"""
         )
     else:
         self.send_response(404)
         self.end_headers()
Beispiel #2
0
    def do_GET(self):
        url = urlparse.urlparse(self.path)
        if url.path == '/metrics':
            params = urlparse.parse_qs(url.query)
            if 'address' not in params:
                self.send_response(400)
                self.end_headers()
                msg = 'Missing "address" from parameters.'
                self.log_message(msg)
                self.wfile.write(msg)
                return

            port = 161
            address = params['address'][0]
            if 'port' in params:
                port = params['port'][0]
            elif ':' in address:
                address, port = address.split(':')

            with open(self._config_path) as f:
                config = yaml.safe_load(f)
            try:
                output = collect_snmp(config, address, port)
            except (ValueError, easysnmp.EasySNMPError) as e:
                self.send_response(502)
                self.end_headers()
                msg = 'Error collecting metrics from remote host: %s' % (e)
                self.log_message(msg)
                self.wfile.write(msg)
                return

            self.send_response(200)
            self.send_header('Content-Type', CONTENT_TYPE_LATEST)
            self.end_headers()
            self.wfile.write(output)
        elif url.path == '/':
            self.send_response(200)
            self.end_headers()
            self.wfile.write("""<html>
      <head><title>SNMP Exporter</title></head>
      <body>
      <h1>SNMP Exporter</h1>
      <p>Usage:<br>
      <code>/metrics?address=1.2.3.4</code><br>
      <code>/metrics?address=1.2.3.4&port=123</code><br>
      <code>/metrics?address=1.2.3.4:123</code></p>
      </body>
      </html>""")
        else:
            self.send_response(404)
            self.end_headers()
Beispiel #3
0
  def do_GET(self):
    url = urlparse.urlparse(self.path)
    if url.path == '/metrics':
      params = urlparse.parse_qs(url.query)
      if 'address' not in params:
        self.send_response(400)
        self.end_headers()
        self.wfile.write("Missing 'address' from parameters")
        return
      with open(self._config_path) as f:
        config = yaml.safe_load(f)

      module = params.get("module", ["default"])[0]
      if module not in config:
        self.send_response(400)
        self.end_headers()
        self.wfile.write("Module '{0}' not found in config".format(module))
        return
      try:
        port = int(params.get('port', (161, ))[0])
        output = collect_snmp(config[module], params['address'][0], port)
        self.send_response(200)
        self.send_header('Content-Type', CONTENT_TYPE_LATEST)
        self.end_headers()
        self.wfile.write(output)
      except:
        self.send_response(500)
        self.end_headers()
        self.wfile.write(traceback.format_exc())
    elif url.path == '/':
      self.send_response(200)
      self.end_headers()
      self.wfile.write("""<html>
      <head><title>SNMP Exporter</title></head>
      <body>
      <h1>SNMP Exporter</h1>
      <p>Visit <code>/metrics?address=1.2.3.4</code> to use.</p>
      </body>
      </html>""")
    else:
      self.send_response(404)
      self.end_headers()
Beispiel #4
0
    def do_GET(self):
        url = urlparse.urlparse(self.path)
        if url.path == '/metrics':
            params = urlparse.parse_qs(url.query)
            if 'address' not in params:
                self.send_response(400)
                self.end_headers()
                self.wfile.write("Missing 'address' from parameters")
                return
            with open(self._config_path) as f:
                config = yaml.safe_load(f)

            module = params.get("module", ["default"])[0]
            if module not in config:
                self.send_response(400)
                self.end_headers()
                self.wfile.write(
                    "Module '{0}' not found in config".format(module))
                return
            try:
                output = collect_snmp(config[module], params['address'][0])
                self.send_response(200)
                self.send_header('Content-Type', CONTENT_TYPE_LATEST)
                self.end_headers()
                self.wfile.write(output)
            except:
                self.send_response(500)
                self.end_headers()
                self.wfile.write(traceback.format_exc())
        elif url.path == '/':
            self.send_response(200)
            self.end_headers()
            self.wfile.write("""<html>
      <head><title>SNMP Exporter</title></head>
      <body>
      <h1>SNMP Exporter</h1>
      <p>Visit <code>/metrics?address=1.2.3.4</code> to use.</p>
      </body>
      </html>""")
        else:
            self.send_response(404)
            self.end_headers()