# How to monitor SIP peer availability with Zabbix

> Monitor SIP peers and Asterisk trunks in Zabbix: qualify, UserParameter, low-level discovery and an availability trigger. Ready-to-use scripts included.

An operator's phone drops its registration and you hear about it from a customer who couldn't get through. A provider trunk hangs overnight and by morning the queue is full of missed calls. Ordinary monitoring covers both: Zabbix asks Asterisk every few seconds whether each SIP peer is reachable and raises an alert before your users notice.

This article walks through the setup from scratch: what to check, how to discover every peer automatically with low-level discovery, and which trigger to use to avoid false alarms.

## What "availability" means for a SIP peer

A peer is an endpoint Asterisk exchanges SIP signalling with: a desk phone, a softphone, a WebRTC client or a provider trunk. "Available" means Asterisk recently got a reply to the SIP `OPTIONS` request it sends itself. This mechanism is called **qualify**.

In PJSIP it is enabled with two AOR parameters:

```ini
[201]
type=aor
qualify_frequency=30
qualify_timeout=5.0
```

Without `qualify_frequency` Asterisk never probes the peer, so the state shown by `pjsip show endpoints` doesn't reflect reality: a contact is considered alive until its registration expires. PearlPBX2 generates both parameters for trunks and for the user AOR template (30 seconds, 5 second timeout), so nothing extra is needed there. On a hand-configured Asterisk, add them to every AOR.

These values set the detection delay: a peer becomes `Unavailable` roughly 30–35 seconds after it disappears. Polling Zabbix more often than that is pointless.

## Step 1. Get the state from Asterisk

The simplest way is the CLI:

```bash
asterisk -rx "pjsip show endpoints"
```

Endpoint lines look roughly like this:

```
 Endpoint:  201/201            Not in use    0 of inf
 Endpoint:  trunk1             Unavailable   0 of inf
```

Only one thing matters: whether the state is `Unavailable`. Anything else (`Not in use`, `In use`, `Ringing`, `Busy`) means the peer is responding.

The `zabbix` user needs access to the Asterisk socket. The cleanest option is to add it to the group Asterisk runs under:

```bash
usermod -aG asterisk zabbix
systemctl restart zabbix-agent2
```

If Asterisk runs in Docker, replace the call with `docker exec <container> asterisk -rx ...` and add `zabbix` to the `docker` group. Keep in mind that this group is effectively root.

## Step 2. The agent script

File `/usr/local/bin/sip_peers.sh`:

```bash
#!/bin/bash
# Usage: sip_peers.sh discover | up <endpoint>
set -u

endpoints() {
  asterisk -rx "pjsip show endpoints" |
    awk '$1=="Endpoint:" && $2 !~ /</ { split($2, a, "/"); print a[1] }'
}

case "${1:-}" in
  discover)
    endpoints | awk 'BEGIN { printf "{\"data\":[" }
      { printf "%s{\"{#PEER}\":\"%s\"}", (NR>1 ? "," : ""), $1 }
      END { print "]}" }'
    ;;
  up)
    asterisk -rx "pjsip show endpoints" |
      awk -v p="$2" '$1=="Endpoint:" { split($2, a, "/");
        if (a[1]==p) { print ($3=="Unavailable" ? 0 : 1); found=1 } }
        END { if (!found) print 0 }'
    ;;
  *) exit 1 ;;
esac
```

`discover` returns the JSON for low-level discovery; `up` returns `1` or `0` for a given peer. If an endpoint disappears from the configuration, `up` returns `0`: a disappearance should not be hidden by silence.

Make the script executable and wire it in as `UserParameter` entries in `/etc/zabbix/zabbix_agent2.d/sip.conf`:

```ini
UserParameter=sip.peer.discovery,/usr/local/bin/sip_peers.sh discover
UserParameter=sip.peer.up[*],/usr/local/bin/sip_peers.sh up $1
```

Test by hand before touching the web interface:

```bash
sudo -u zabbix zabbix_agent2 -t sip.peer.discovery
sudo -u zabbix zabbix_agent2 -t 'sip.peer.up[201]'
```

## Step 3. The Zabbix template

Create a template called `Asterisk SIP peers` and add:

1. **Discovery rule**: key `sip.peer.discovery`, interval `1m`.
2. **Item prototype**: key `sip.peer.up[{#PEER}]`, type "Zabbix agent", value type Numeric (unsigned), interval `30s`.
3. **Trigger prototype**:

```
max(/Asterisk SIP peers/sip.peer.up[{#PEER}],3m)=0
```

The trigger fires only if there hasn't been a single `1` in the last three minutes. One lost `OPTIONS` caused by a small network glitch won't raise an alert, while a real outage (phone switched off, trunk stopped answering) is caught within three to four minutes.

That is too slow for trunks. Use a filter in the discovery rule and make two sets of prototypes: for names matching `^trunk`, a `1m` window and High severity; for everything else, `5m` and Warning. For this to work, agree on a consistent trunk naming scheme up front.

## Step 4. Don't forget trunk registration

If Asterisk registers with the provider itself, `Unavailable` isn't the only way a trunk can break: the provider may answer `OPTIONS` but reject `REGISTER`. Add one more item:

```bash
asterisk -rx "pjsip show registrations"
```

The last field of each line is the state: `Registered`, `Rejected` or `Unregistered`. Convert it to `1`/`0` the same way as above and put a trigger on `0`.

## Scaling

The script calls Asterisk separately for every item. That is fine for dozens of peers but wasteful for hundreds. In that case, create one master item that returns the whole output of `pjsip show endpoints` every 30 seconds and build dependent items with preprocessing on top of it. Asterisk is then polled once per cycle.

## Summary

- Without `qualify_frequency`, a peer's state in Asterisk can't be trusted, so check that first.
- One script and two `UserParameter` entries give automatic discovery of every endpoint, trunks included.
- The `max(...,3m)=0` trigger filters out short glitches; shorten the window for trunks.
- Monitor trunk registration separately: a reply to `OPTIONS` doesn't prove outbound calling works.

Zabbix answers "what broke". To see what is happening with calls right now (queues, active conversations, paused agents), PearlPBX2 has a live dashboard with a PJSIP tab that shows trunk and internal-user presence in BLF style.
