From 416a6397f5963c4021f380af97b7bbff1fb71239 Mon Sep 17 00:00:00 2001
From: Quentin Duchemin <quentinduchemin@tuta.io>
Date: Thu, 28 May 2020 22:55:25 +0200
Subject: [PATCH] Add total users stat to Etherpad

---
 README.md            |  1 +
 etherpad/etherpad.py | 21 +++++++++++++++++----
 2 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/README.md b/README.md
index 7c96717..6a1593f 100644
--- a/README.md
+++ b/README.md
@@ -50,6 +50,7 @@ Pay attention that it is a **list** of objects (wich contains URL and name of yo
 Etherpad module exports following metrics :
 - `etherpad_pads_count` : Number of pads on the instance
 - `etherpad_blank_pads_count` : Number of blank pads on the instance
+- `etherpad_total_users` : Number of connected users
 Each metric have a `name` tag with the name of the instance.
 
 
diff --git a/etherpad/etherpad.py b/etherpad/etherpad.py
index e715eab..eea7a6d 100644
--- a/etherpad/etherpad.py
+++ b/etherpad/etherpad.py
@@ -65,6 +65,16 @@ class EtherpadCollector(object):
                     'value': data['blankPads']
                 }
             })
+            metrics.append({
+                'measurement': 'etherpad_total_users',
+                'tags': {
+                    'name': instance['name']
+                },
+                'time': data['timestamp']*1000,
+                'fields': {
+                    'value': data['totalUsers']
+                }
+            })
 
         return metrics
 
@@ -76,12 +86,15 @@ class EtherpadCollector(object):
         :returns: JSON data returned by Etherpad stats module
         """
         # Send request
-        res = requests.get(instance['url'] + "/stats.json")
-        if res.status_code != 200:
+        stats_plugin = requests.get(instance['url'] + "/stats.json")
+        stats_native = requests.get(instance['url'] + "/stats")
+        if stats_plugin.status_code != 200 or stats_native.status_code != 200:
             return None
         try:
-            data = json.loads(res.text)
+            plugin = json.loads(stats_plugin.text)
+            native = json.loads(stats_native.text)
+            plugin['totalUsers'] = native['totalUsers']
+            return plugin
         except IOError as err:
             print(err)
             return None
-        return data
-- 
GitLab