Skip to content

Commit

Permalink
Implement mi_http to support OpenSIPS >= 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
RubenHoms committed Nov 5, 2020
1 parent 152f948 commit be37f63
Show file tree
Hide file tree
Showing 14 changed files with 1,385 additions and 27 deletions.
35 changes: 29 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
FROM quay.io/prometheus/busybox:latest

This comment has been minimized.

Copy link
@hafkensite

hafkensite Nov 6, 2020

Contributor

Seems that instead of only running the exporter in this docker image, it also includes OpenSIPS itself. I think that is not required

This comment has been minimized.

Copy link
@RubenHoms

RubenHoms Nov 6, 2020

Author Contributor

Yes I did this so I could test against OpenSIPS 3.0 which was very helpful because I could run the exporter locally with debug functionality.

But you're right I should seperate the two, maybe keep this as the 'test' Dockerfile, and have the main one just run the exporter.

MAINTAINER The Prometheus Authors <[email protected]>
FROM debian:buster
LABEL maintainer="Ruben Homs <[email protected]>"

COPY opensips_exporter /bin/opensips_exporter
USER root

ENTRYPOINT ["/bin/opensips_exporter"]
USER nobody
EXPOSE 9434
# Set Environment Variables
ENV DEBIAN_FRONTEND noninteractive

ARG OPENSIPS_VERSION=3.0
ARG OPENSIPS_BUILD=releases

#install basic components
RUN apt update -qq && apt install -y gnupg2 ca-certificates

#add keyserver, repository
RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 049AD65B
RUN echo "deb https://apt.opensips.org buster ${OPENSIPS_VERSION}-${OPENSIPS_BUILD}" >/etc/apt/sources.list.d/opensips.list

RUN apt update -qq && apt install -y opensips curl net-tools procps

RUN apt-get -y install opensips-http-modules

RUN rm -rf /var/lib/apt/lists/*

EXPOSE 5060/udp
EXPOSE 8888/tcp

COPY run.sh /run.sh
COPY opensips.cfg /etc/opensips/opensips.cfg

ENTRYPOINT ["/run.sh"]
39 changes: 35 additions & 4 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ DOCKER_IMAGE_TAG ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD))
MACH ?= $(shell uname -m)
DOCKERFILE ?= Dockerfile

NAME ?= opensips
OPENSIPS_DOCKER_TAG ?= 2.4.0

all: format vet staticcheck build

style:
Expand Down Expand Up @@ -58,6 +61,15 @@ docker:
@echo ">> building docker image from $(DOCKERFILE)"
@docker build --file $(DOCKERFILE) -t "$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" .

docker-build:
docker build \
--tag="opensips/opensips:$(OPENSIPS_DOCKER_TAG)" \
.

start:
docker run -p 8888:8888 -d --name $(NAME) opensips/opensips:$(OPENSIPS_DOCKER_TAG) -v /home/ruben/go/src/github.com/VoIPGRID/opensips_exporter/:/tmp/


$(GOPATH)/bin/promtool promtool:
@GOOS= GOARCH= $(GO) get -u github.com/prometheus/prometheus/cmd/promtool

Expand Down
38 changes: 31 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
This exporter exposes OpenSIPS metrics for consumption by Prometheus using the Unix socket
provided by OpenSIPS. It uses the
OpenSIPS [Management Interface](http://www.opensips.org/Documentation/Interface-MI-2-4) to gather
these statistics.
these statistics. It supports two protocols to communicate with the Management Interface `mi_datagram` for OpenSIPS versions up to 2.4.x and for OpenSIPS versions from 3.0 and higher it supports `mi_http` (JSON-RPC)

Tested and developed against OpenSIPS versions 1.10 and 2.4, though this will probably work with all other versions as well.
Tested and developed against OpenSIPS versions 1.10, 2.4, 3.0, 3.1 though this will probably work with all other versions as well.

## Status

Expand Down Expand Up @@ -32,19 +32,42 @@ Make sure `$GOPATH/bin` is in your `$PATH`.

```text
Usage of opensips_exporter:
-path string
The path where metrics will be served (default "/metrics")
-addr string
Address on which the OpenSIPS exporter listens. (e.g. 127.0.0.1:9434) (default ":9434")
Address on which the OpenSIPS exporter listens. (e.g. 127.0.0.1:9434) (default ":9434")
-http_address string
Address to query the management query through HTTP (e.g. http://127.0.0.1:8888/mi/) (default "http://127.0.0.1:8888/mi/")
-path string
The path where metrics will be served. (default "/metrics")
-protocol string (required)
Which protocol to use to get data from the Management Interface (mi_datagram & mi_http currently supported) (default "mi_datagram")
-socket string
Path to the socket file for OpenSIPS. (default "/var/run/ser-fg/ser.sock")
Path to the socket file for OpenSIPS.) (default "/var/run/ser-fg/ser.sock")
```

For your openSIPS instance make sure that you have the `mi_datagram` module loaded and defined the location of the socket like so.
### OpenSIPS up to version 2.4
Up to OpenSIPS version 2.4 the exporter works with the `mi_datagram` module. You can load it in your OpenSIPS config like so:
```
loadmodule "mi_datagram.so"
modparam("mi_datagram", "socket_name", "RUNDIR/ser.sock")
```
Then start the exporter with the following params:
```
opensips_exporter -protocol mi_datagram -socket RUNDIR/ser.sock
```

### OpenSIPS version 3.0 and higher
From OpenSIPS version 3.0 and higher the datagram protocol is not supported, instead you can use the `mi_http` module
which uses JSON-RPC to communicate with the Management Interface. For debian you have to install
the `opensips-http-modules` to include the module in your OpenSIPS installation. You can load it in your OpenSIPS config like so:
```
loadmodule "httpd.so"
loadmodule "mi_http.so"
modparam("httpd", "ip", "127.0.0.1")
```
By default the management interface listens on port 8888 which is the default in the exporter as well. You can start the exporter with the following params:
```
opensips_exporter -protocol mi_http
```

## Exported Metrics

Expand Down Expand Up @@ -73,6 +96,7 @@ modparam("mi_datagram", "socket_name", "RUNDIR/ser.sock")
| opensips_load_all_10m | The last 10 minute average load of entire OpenSIPS (covering all processes). (**OpenSIPS >= 2.4**) | | Gauge |
| opensips_load_1m | The last minute average load of the process ID. (**OpenSIPS >= 2.4**) | ip, port, protocol, process | Gauge |
| opensips_load_10m | The last 10 minute average load of the process ID. (**OpenSIPS >= 2.4**) | ip, port, protocol, process | Gauge |
| opensips_load_processes_number | Number of running OpenSIPS processes. (**OpenSIPS >= 3.0**) | | Gauge |
| opensips_net_waiting | Number of bytes waiting to be consumed on an interface that OpenSIPS is listening on. | protocol | Gauge |
| opensips_pkmem_fragments | Currently available number of free fragments in the private memory for OpenSIPS process. | pid | Gauge |
| opensips_pkmem_free_size | Free private memory available for the OpenSIPS process. Computed as total_size - real_used_size. | pid | Gauge |
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.1
2.0.0
Loading

4 comments on commit be37f63

@hafkensite
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is are a few go vet warnings:

$ make vet                     
>> vetting code
# github.com/VoIPGRID/opensips_exporter/opensips
opensips/opensips.go:103:12: result of fmt.Errorf call not used
# github.com/VoIPGRID/opensips_exporter/processors
processors/load_processor.go:242:12: result of fmt.Errorf call not used
make: *** [Makefile:46: vet] Error 2

@hafkensite
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a next step I would like to convert this to use go mod, and have the vendor directory removed. Maybe even use goreleaser to build the docker images and debian packages.

@hafkensite
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you choose github.com/KeisukeYamashita/go-jsonrpc over https://golang.org/pkg/net/rpc/jsonrpc/?

@RubenHoms
Copy link
Contributor Author

@RubenHoms RubenHoms commented on be37f63 Nov 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you choose github.com/KeisukeYamashita/go-jsonrpc over https://golang.org/pkg/net/rpc/jsonrpc/?

Hadn't realised there was a native package to do this, but I used this package because of this comment that helped me start the process of adding this functionality. Looking at the native package it says:

Package jsonrpc implements a JSON-RPC 1.0 ClientCodec and ServerCodec for the rpc package.

OpenSIPS implements JSON-RPC 2.0, which apparently doesn't have a native package yet so it wouldn't work with it either.

Please sign in to comment.