[TUTORIAL] PMG python api monitoring + Zabbix Auto discovery

ananiasfilho

Active Member
Nov 20, 2019
27
29
33
44
[UPDATE] - PMG and PVE support on Proxmoxer

Our team made modifications on proxmoxer and propose a Pull Request, while it not official, all of you can use our FORK proxmoxer.

https://github.com/kmee/proxmoxer/

All details how to use are described on README.

Short usage information
The first thing to do is import the proxmoxer library and create ProxmoxAPI instance.

from proxmoxer import ProxmoxAPI
proxmox = ProxmoxAPI('proxmox_host', user='admin@pam',
password='secret_word', verify_ssl=False)
This will connect by default to PVE through the 'https' backend.

To define service to PVE or PMG, include service option into script:

Define PVE connection:

from proxmoxer import ProxmoxAPI
proxmox = ProxmoxAPI('proxmox_host', user='admin@pam',
password='secret_word', verify_ssl=False, service='PVE')
Define PMG connection:

from proxmoxer import ProxmoxAPI
proxmox = ProxmoxAPI('proxmox_host', user='admin@pam',
password='secret_word', verify_ssl=False, service='PMG')


Be happy! :) Enjoy it!
[Original Thread and steps to modify Proxmoxer]
I have worked on PMG monitoring with Zabbix + Grafana and changed PVE proxmoxer[1] api to work with PMG.
I have opened a Feature request [2] on Proxmoxer lib to add custom field to support PMG without changes on Proxmoxer.

While it not done by developers, let show how to change Proxmoxer to work with PMG

To proxmoxer lib work with proxmox mail gateway i just changed line bellow:
https://github.com/proxmoxer/proxmoxer/blob/develop/proxmoxer/backends/https.py#L83
return cookiejar_from_dict({"PVE"AuthCookie: self.pve_auth_ticket})
TO
return cookiejar_from_dict({"PMG"AuthCookie: self.pve_auth_ticket})

To install proxmoxer and work with PMG i recommend to install it under virtualenv. So, follow steps:
1 - mkdir /opt/pmg_api (or other directory)
2 - cd /opt/pmg_api
3 - apt install virtualenv
4 - virtualenv -p python3 .
5 - bin/pip install requests proxmoxer
6 - sed -i 's/PVEAuthCookie/PMGAuthCookie/g' ./lib/python3.6/site-packages/proxmoxer/backends/https.py

Create a script to show all senders statistics

Example: All users statistics - /api2/json/statistics/sender
Reference: https://pmg.proxmox.com/pmg-docs/api-viewer/index.html#/statistics/sender

Script senders.py
### Start script
#!/opt/pmg_api/python
from proxmoxer import ProxmoxAPI
import json
proxmox = ProxmoxAPI('pmg.mydomain.com', user='root@pam',
password='my_password', verify_ssl=False)

a = proxmox.statistics.sender.get()
b = json_formatted_str = json.dumps(a, indent=2)
print(b)
### End script

Run senders.py
bin/python senders.py

Result:
[
{
"bytes": 4761,
"count": 5,
"sender": "user_x@domain1.com",
"spamcount": 0,
"viruscount": 0
},
{
"bytes": 497776,
"count": 3,
"sender": "user_a@domain2.com",
"spamcount": 0,
"viruscount": 0
}
]

So you can parser it to zabbix using LLD Autodiscovery in zabbix or any other monitoring system (or web page that load json to human readable)

Example to parser it as zabbix LLD - Not so good yet, but works fine!
### Start script
#!/opt/pmg_api/python
from proxmoxer import ProxmoxAPI
import json
proxmox = ProxmoxAPI('pmg.mydomain.com', user='root@pam',
password='my_password', verify_ssl=False)

a = proxmox.statistics.sender.get()
b = json_formatted_str = json.dumps(a, indent=2, sort_keys=True)
c = b.replace('"sender"', '"{#SENDER}"')
d = c.replace('"count"', '"{#COUNT}"')
e = d.replace('"bytes"', '"{#BYTES}"')
f = e.replace('"spamcount"', '"{#SPAMCOUNT}"')
g = f.replace('"viruscount"', '"{#VIRUSCOUNT}"')
print('{\n "data": ')
print(g)
print("}")
### End script

chmod +x /opt/pmg_api/senders.py
# Add follow UserParameters into your zabbix_agentd.conf
### Discovery users
UserParameter=pmg.senders.discovery, /opt/pmg_api/senders.py
### Update sent emails for each user
UserParameter=pmg.senders.[*], cat /tmp/count.csv |grep "$1" |cut -d"," -f2

Create another script to send email and count details to a temporary csv file so zabbix can read data.
count.py script

#!/opt/pmg_api/bin/python
from proxmoxer import ProxmoxAPI
import json
import csv
proxmox = ProxmoxAPI('pmg.mydomain.com', user='root@pam',
password='my_password', verify_ssl=False)

a = proxmox.statistics.sender.get()
b = json_formatted_str = json.dumps(a, indent=2, sort_keys=True)
for sender in a:
print("%s,%s" % (sender['sender'], sender['count']))

Set execution permission
chmod +x /opt/pmg_api/count.py

Now, add this script to root or zabbix cron:
* * * * * /opt/pmg_api/count.py > /tmp/count.csv

Download and import zabbix template from my repository on github.
https://github.com/ananiasfilho/zabbix_pmg

Restart Zabbix agent and wait few minutes. Go to monitoring -> Latest data -> select your zabbix server host and find emails account and count emails.

[1] - https://github.com/proxmoxer/proxmoxer
[2] - https://github.com/proxmoxer/proxmoxer/issues/28
 
Last edited:
Thanks for sharing your modifications!

Im working on a complete monitoring system to PMG and creating a Grafana dashboard so SysAdmins can take a fast look of yours PMG and make alerts on it.
Example:
Triggers/Alerts:
- when user account have more than 10 emails/minute
- when queue is more than X mails on last 1 hour
and SysAdmins can make a lot of triggers on zabbix to make sure your service is running as expect.
Thank you for your comment and all are invited to contribute!
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!