利用prometheus文件服務(wù)發(fā)現(xiàn)功能遂鹊,可以方便自定義主機(jī)或虛擬機(jī)的監(jiān)控振乏。
1. /etc/prometheus/prometheus.yml配置文件
文件服務(wù)發(fā)現(xiàn)配置參考 job_name: 'vm' 段
# my global configglobal:? scrape_interval:15s# Set the scrape interval to every 15 seconds. Default is every 1 minute.? evaluation_interval:15s# Evaluate rules every 15 seconds. The default is every 1 minute.# scrape_timeout is set to the global default (10s).# Attach these labels to any time series or alerts when communicating with# external systems (federation, remote storage, Alertmanager).? external_labels:? ? ? monitor:'codelab-monitor'# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.rule_files:# - "first.rules"# - "second.rules"# A scrape configuration containing exactly one endpoint to scrape:# Here it's Prometheus itself.scrape_configs:# The job name is added as a label `job=` to any timeseries scraped from this config.? - job_name:'prometheus'# metrics_path defaults to '/metrics'# scheme defaults to 'http'.? ? static_configs:? ? ? - targets:['localhost:9090']scrape_configs:? - job_name:'vm'? ? file_sd_configs:? ? ? - files:? ? ? ? ? -/etc/prometheus/files/vm.yml? ? ? ? refresh_interval:5s
2. /etc/prometheus/files/vm.yml配置文件內(nèi)容
[? {"targets": ["192.168.1.6:9273"],"labels": {"hosttype":"vm"}? }]
3. 通過docker啟動(dòng)prometheus
docker run-d-p9090:9090\-v/etc/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \-v/etc/prometheus/files:/etc/prometheus/files \prom/prometheus:v1.8.2
4. 檢查prometheus的target,是否包含files.yml文件中配置的target
http://192.168.1.6:9090/targets
5.動(dòng)態(tài)生成/etc/prometheus/files.yml?
如果想動(dòng)態(tài)修改target秉扑,可以通過http請求慧邮,獲取主機(jī)列表,然后動(dòng)態(tài)生成/etc/prometheus/files/vm.yml文件舟陆。
下面是動(dòng)態(tài)生成vm.yml例子误澳。通過http請求,獲取虛擬機(jī)的ip地址列表秦躯,然后動(dòng)態(tài)生成vm.yml文件忆谓。
#!/usr/bin/pythonimportjsonimportloggingimportosimportrequestsimporttimelogging.basicConfig(level=logging.INFO,? ? ? ? ? ? ? ? ? ? format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',? ? ? ? ? ? ? ? ? ? datefmt='%Y-%m-%d %H:%M:%S',? ? ? ? ? ? ? ? ? ? filename='/var/log/prometheus-vm-http.log',? ? ? ? ? ? ? ? ? ? filemode='w')console = logging.StreamHandler()console.setLevel(logging.INFO)formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')console.setFormatter(formatter)logging.getLogger('').addHandler(console)def refresh():url ="http://域名/api/vm/ip"resp = requests.get(url, timeout=10)ifresp.status_code ==200:? ? ? ? logging.info("get "+ url +" ok")? ? ? ? data = [? ? ? ? ? ? {"targets": map(lambdax: x +":9273", resp.json()),"labels": {"hosttype":"vm"}? ? ? ? ? ? }? ? ? ? ]withopen('/etc/prometheus/files/vm.yml.new','w')asf:? ? ? ? ? ? json.dump(data, f, indent=2)? ? ? ? ? ? f.flush()? ? ? ? ? ? os.fsync(f.fileno())? ? ? ? os.rename('/etc/prometheus/files/vm.yml.new','/etc/prometheus/files/vm.yml')else:? ? ? ? logging.error("get "+ url +" failed. status_code=%d, resp=%s"% (resp.status_code, resp.text))if__name__ =='__main__':whileTrue:try:? ? ? ? ? ? refresh()exceptException, e:? ? ? ? ? ? logging.error(e)? ? ? ? time.sleep(10)