Pushgateway is a component of Prometheus. Prometheus server actively obtains data through Exporter by default (pull is used to pull data by default), and Pushgateway actively pushes data to Pushgateway through exporter, and then Prometheus actively pulls Pushgateway data. Users can write Some custom monitoring scripts send the data to be monitored to Pushgateway. From the perspective of the Prometheus server, it is the Prometheus server that actively pulls data from various data sources (such as Exporter and Pushgateway).
Advantages of Pushgateways:
Prometheus uses the timing pull mode to pull the targets data by default, but if it is not in a subnet or a firewall, Prometheus cannot pull the targets data, so you can use each target to push data on the pushgateway, and then Prometheus goes to the Pushgateway to pull the data regularly;
When monitoring business data, it is necessary to summarize different data. The summarized data can be collected by Pushgateway, and then pulled by Prometheus to relieve pressure on Prometheus;
Custom collection indicators are simple.
Disadvantages of Pushgateways:
Prometheus pull status is only for Pushgateway, not valid for every node;
If there is a problem with the Pushgateway, there will be a problem with the entire collected data;
Pushgateway can persist all the monitoring data pushed to it, so even if the monitoring is offline, Prometheus will still pull the old monitoring data, and you need to manually clean up the data that Pushgateway does not want.
For in-depth learning of Prometheus, please refer to:
Pushgateway is a data transfer station that provides APIs to support data producers to push data over at any time.
Pushgateway provides the exporter function, and when the Promethus server pulls data, it feeds back the data saved by itself to the Promethus server.
3. Prometheus server installation
① Download
Prometheus is written based on Golang. The compiled software package does not depend on any third-party dependencies. You only need to download the binary package of the corresponding platform, decompress it and add basic configuration to start Prometheus Server normally: (download address: Prometheus server ) :
wget https://github.com/prometheus/prometheus/releases/download/v2.40.6/prometheus-2.40.6.linux-amd64.tar.gz
tar -xf prometheus-2.40.6.linux-amd64.tar.gz
② Configuration
After decompression, the current directory will contain the default Prometheus configuration file promethes.yml. The following configuration file is briefly analyzed:
Check the prometheus web interface http://ip:9090/targets again:
5. Practical operation demonstration
① Push data
The URL part of the push path is defined as (the job is a required parameter, the label_name part is optional, and the combination of job and label in the URL uniquely identifies the Group in the pushgateway):
Data is deleted in units of Group, which is uniquely identified by the job name and the label in the URL;
The statement to delete {job="some_job"} data in the example will not delete {job="some_job", instance="some_instance"} data, because they belong to different groups. If you need to delete {job="some_job", instance ="some_instance"}, you need to use;
Deleting data refers to deleting the data in the pushgateway, which has nothing to do with promethues.
③ Customize the method of writing scripts, send pushgateway collection
The template is as follows:
cat <<EOF| curl --data-binary @- http://192.168.182.110:9091/metrics/job/some_job/instance/some_instance
# A histogram, which has a pretty complex representation in the text format:
# HELP http_request_duration_seconds A histogram of the request duration.
# TYPE http_request_duration_seconds histogram
disk_usage{
instance="local-168-182-110",job="disk",disk_name="/run/user/0"}11
disk_usage{
instance="local-168-182-110",job="disk",disk_name="/run/user/1"}22
disk_usage{
instance="local-168-182-110",job="disk",disk_name="/run/user/2"}33
disk_usage{
instance="local-168-182-110",job="disk",disk_name="/run/user/3"}44
disk_usage{
instance="local-168-182-110",job="disk",disk_name="/run/user/4"}55EOF
Write a collection script to push data to Pushgateway:
cat >disk_usage_metris.sh<<EOF
#!/bin/bash
hostname=`hostname -f | cut -d '.'-f1`
metrics=""for line in `df |awk 'NR>1{
print $NF"="int($(NF-1))}'`
do
disk_name=`echo $line|awk -F'=' '{
print $1}'`
disk_usage=`echo $line|awk -F'=' '{
print $2}'`
metrics="$metrics\ndisk_usage{instance=\"$hostname\",job=\"disk\",disk_name=\"$disk_name\"} $disk_usage"
done
echo -e "# A histogram, which has a pretty complex representation in the text format:\n# HELP http_request_duration_seconds A histogram of the request duration.\n# TYPE http_request_duration_seconds histogram\n$metrics"| curl --data-binary @- http://192.168.182.110:9091/metrics/job/pushgateway/instance/disk_usage
EOF