├── Dockerfile ├── LICENSE ├── README.md └── files └── progs └── nginx.mtail /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM alpine:latest 2 | 3 | EXPOSE 3093 4 | 5 | ENV GOPATH /go 6 | ENV PATH /go/bin:$PATH 7 | ENV SRC_PATH /go/src/github.com/google/mtail 8 | 9 | RUN apk add --update musl go git bash make && \ 10 | mkdir -p $SRC_PATH && cd $SRC_PATH/../ && \ 11 | git clone https://github.com/google/mtail.git && \ 12 | cd $SRC_PATH && git reset --hard 65d3620c858829c218370b0616ca0438add23186 && \ 13 | make install_coverage_deps && make install_gen_deps && make install && \ 14 | mv `which mtail` /usr/local/bin/mtail && \ 15 | apk del --purge musl go git && rm -rf $GOPATH 16 | 17 | COPY files/ /mtail/ 18 | 19 | ENTRYPOINT ["/usr/local/bin/mtail", "-v=2", "-logtostderr", "-port", "3093", "-progs", "/mtail/progs", "-logs"] 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Nelson Diaz 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nginx-prometheus-exporter 2 | Nginx metrics exporter for Prometheus. 3 | Based on @ipfs mtail container, tails nginx logs and exports calculated metrics in Prometheus and json formats. 4 | ## Nginx Settings 5 | Make sure your nginx logs follow this format: 6 | ``` 7 | log_format mtail '$host $remote_addr - $remote_user [$time_local] ' 8 | '"$request" $status $body_bytes_sent $request_time ' 9 | '"$http_referer" "$http_user_agent" "$content_type"'; 10 | ``` 11 | In your server declaration block: 12 | ``` 13 | access_log /var/log/nginx/access.log mtail; 14 | ``` 15 | ## Run 16 | ``` 17 | docker run -d \ 18 | -v /var/log/nginx:/var/log/nginx:ro \ 19 | -p 3093:3093 \ 20 | ndiazg/nginx-prometheus-exporter \ 21 | /var/log/nginx/access.log 22 | ``` 23 | ## Scrape 24 | Prometheus format 25 | ``` 26 | http://your-host:3093/metrics 27 | ``` 28 | Json format 29 | ``` 30 | http://your-host:3093/json 31 | ``` 32 | -------------------------------------------------------------------------------- /files/progs/nginx.mtail: -------------------------------------------------------------------------------- 1 | counter http_requests_total by vhost, method, code, content_type 2 | counter http_request_duration_milliseconds_sum by vhost, method, code, content_type 3 | counter http_response_size_bytes_sum by vhost, method, code, content_type 4 | 5 | # log_format mtail '$server_name $remote_addr - $remote_user [$time_local] ' 6 | # '"$request" $status $bytes_sent $request_time' 7 | # '"$http_referer" "$http_user_agent" "$content_type"'; 8 | 9 | /^/ + 10 | /(?P[0-9A-Za-z\.\-:]+) / + 11 | /(?P[0-9A-Za-z\.\-:]+) / + 12 | /- / + 13 | /(?P[0-9A-Za-z\-]+) / + 14 | /(?P\[\d{2}\/\w{3}\/\d{4}:\d{2}:\d{2}:\d{2} [\-\+]\d{4}\]) / + 15 | /"(?P[A-Z]+) (?P\S+) (?PHTTP\/[0-9\.]+)" / + 16 | /(?P\d{3}) / + 17 | /(?P\d+) / + 18 | /(?P\d+)\.(?P\d+) / + 19 | /"(?P\S+)" / + 20 | /"(?P[[:print:]]+)" / + 21 | /"(?P[^;\\]+)(;.*)?"/ + 22 | /$/ { 23 | http_requests_total[$vhost][tolower($request_method)][$status][$content_type]++ 24 | http_request_duration_milliseconds_sum[$vhost][tolower($request_method)][$status][$content_type] += $request_seconds * 1000 + $request_milliseconds 25 | http_response_size_bytes_sum[$vhost][tolower($request_method)][$status][$content_type] += $bytes_sent 26 | } 27 | --------------------------------------------------------------------------------