mgcp_ratectr: add stats items to monitor trunk usage

We are currently counting events in rate counters, but there is
currently no way to get a sample of the current situation of the trunk
usage. In particular how many endpoints are currently in use.

This is a corrected version of:
Ib7b654168dc3512f55e45cc4755dc1f6f423d023

Change-Id: I6d3a74f6087512130d85002348787bffc672de81
Related: SYS#5201
diff --git a/src/libosmo-mgcp/mgcp_endp.c b/src/libosmo-mgcp/mgcp_endp.c
index 16043d4..4fcddb8 100644
--- a/src/libosmo-mgcp/mgcp_endp.c
+++ b/src/libosmo-mgcp/mgcp_endp.c
@@ -29,6 +29,7 @@
 
 #include <osmocom/abis/e1_input.h>
 #include <osmocom/mgcp/mgcp_e1.h>
+#include <osmocom/core/stat_item.h>
 
 #define E1_RATE_MAX 64
 #define E1_OFFS_MAX 8
@@ -122,6 +123,12 @@
 	 * RSIP is executed), free them all at once. */
 	mgcp_conn_free_all(endp);
 
+	/* We must only decrement the stat item when the endpoint as actually
+	 * claimed. An endpoint is claimed when a call-id is set */
+	if (endp->callid)
+		osmo_stat_item_dec(osmo_stat_item_group_get_item(endp->trunk->stats.common,
+								 TRUNK_STAT_ENDPOINTS_USED), 1);
+
 	/* Reset endpoint parameters and states */
 	talloc_free(endp->callid);
 	endp->callid = NULL;
@@ -598,6 +605,8 @@
 	 * connection ids) */
 	endp->callid = talloc_strdup(endp, callid);
 	OSMO_ASSERT(endp->callid);
+	osmo_stat_item_inc(osmo_stat_item_group_get_item(endp->trunk->stats.common,
+							 TRUNK_STAT_ENDPOINTS_USED), 1);
 
 	/* Allocate resources */
 	switch (endp->trunk->trunk_type) {