first commit
This commit is contained in:
		
							
								
								
									
										70
									
								
								carp_exporter.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										70
									
								
								carp_exporter.py
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,70 @@ | |||||||
|  | #!/usr/local/bin/python3.8 | ||||||
|  | # | ||||||
|  | # Prometheus exporter for CARP VIP Status | ||||||
|  | # | ||||||
|  | # johan@nosd.in 19/08/2021 | ||||||
|  | # | ||||||
|  | # v.0.9 20/08/2021 First version | ||||||
|  | # v.1.0 10/05/2022 Fix for more than 1 VIP | ||||||
|  | # v.1.1 11/05/2022 Fix for INIT status VIP | ||||||
|  | # | ||||||
|  |  | ||||||
|  | import http.server | ||||||
|  | import socketserver | ||||||
|  | from subprocess import Popen, PIPE | ||||||
|  | import io | ||||||
|  | import re | ||||||
|  |  | ||||||
|  | PORT = 8992 | ||||||
|  |  | ||||||
|  | # Identify carp status line in ifconfig output | ||||||
|  | CARP_STATUS_REGEX="^\s*carp:\s(MASTER|BACKUP|INIT)\svhid\s([0-9]*)\sadvbase\s([0-9]*)\sadvskew\s([0-9]*)$" | ||||||
|  | # Identify IP assigned to VHID. {{VHID}} will be templated with vhid value | ||||||
|  | CARP_VIP_REGEX_GEN="^\s*inet\s([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\snetmask\s(0x[0-9a-f]{8})\sbroadcast\s([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\svhid\s{{VHID}}$" | ||||||
|  |  | ||||||
|  | #if __name__ == '__main__': | ||||||
|  |  | ||||||
|  | regex_sta = re.compile(CARP_STATUS_REGEX) | ||||||
|  |  | ||||||
|  | class RequestHandler(http.server.SimpleHTTPRequestHandler): | ||||||
|  |   def do_GET(self): | ||||||
|  |     if self.path == "/metrics": | ||||||
|  |       lines = [] | ||||||
|  |       vip = "" | ||||||
|  |       proc = Popen(['ifconfig'], stdout=PIPE, stderr=PIPE) | ||||||
|  |       for line in io.TextIOWrapper(proc.stdout, encoding="utf-8"): | ||||||
|  |         lines.append(line) | ||||||
|  |  | ||||||
|  |       body = "" | ||||||
|  |       for l1 in lines: | ||||||
|  |         if 'carp' in l1: | ||||||
|  |           m1 = regex_sta.search(l1) | ||||||
|  |           if len(m1.groups()) != 4: | ||||||
|  |             print('Unable to get CARP status') | ||||||
|  |             self.send_response(500) | ||||||
|  |             break | ||||||
|  |           else: | ||||||
|  |             cvip_regex = str.replace(CARP_VIP_REGEX_GEN, "{{VHID}}", m1.group(2)) | ||||||
|  |             re_vip = re.compile(cvip_regex) | ||||||
|  |             buf = 'vhid ' + m1.group(2) | ||||||
|  |             for l2 in lines: | ||||||
|  |               if buf in l2: | ||||||
|  |                 m2 = re_vip.search(l2.rstrip()) | ||||||
|  |                 if m2 is not None: | ||||||
|  |                   vip = m2.group(1) | ||||||
|  |             if len(vip) > 0: | ||||||
|  |               buf = 'carp_vip_status_up{vhid="' + m1.group(2) + '",status="' + m1.group(1) + '",vip="' + vip + '"} 1\n' | ||||||
|  |               body += buf | ||||||
|  |               vip = "" | ||||||
|  |             else: | ||||||
|  |               print("Unable to get vhid " + ml.group(2) + " assigned IP") | ||||||
|  |       if len(body) > 0: | ||||||
|  |         self.wfile.write("HTTP/1.0 200 OK\n\n".encode() + body.encode()) | ||||||
|  |         self.send_response(200) | ||||||
|  |     else: | ||||||
|  |       self.send_response(200) | ||||||
|  |  | ||||||
|  | with socketserver.TCPServer(("", PORT), RequestHandler) as httpd: | ||||||
|  |   print("Serving at port", PORT) | ||||||
|  |   httpd.serve_forever() | ||||||
|  |  | ||||||
		Reference in New Issue
	
	Block a user