Foreword
In my first
publication, I talked about how you can configure the monitoring of the queues of the Communigate Pro mail servers (CGP) in Zabbix. Today I will talk about my little experience of using
low-level discover (LLD) Zabbix to monitor the number of users in domains. I must say that the practical sense of monitoring the number of users, in my opinion, is small. This was done more for our own joy and the ability to quickly answer the authorities the question "how many users do we have?" Without having to run through all the servers.
Low-level discover
LLD provides the ability to automatically create data items, triggers and graphics for various computer settings. For example, Zabbix can automatically collect data about your file system or network interfaces and build data elements for monitoring based on this information.
In order for LLD to be possible, we need to add a custom parameter to the Zabbix agent settings, the request of which will return to the monitoring server a list of JSON objects, each of which in the case of, for example, network interfaces, corresponds to one interface.
Task
It is necessary to make it possible to “monitor” the number of users in all domains of the CGP server + the total number of users on the server.
The implementation of this task consists of the following steps:
1. Script for getting the list of CGP domains for LLD.
2. Script to get the number of accounts for the domain.
3. Place these scripts on the server.
4. Configure Zabbix agent.
5. Create a template in Zabbix to monitor the number of accounts.
6. Add this template to the necessary nodes in Zabbix.
Important note! Scripts that are used in this case require the user, whose name they use in their work, to have the “Can Modify All Domains and Accounts Settings” permission (see more
here ). These are very serious rights that allow you to simply clear a domain from users. Accordingly, everything that you do - you do at your own peril and risk.
')
1. Script for getting the list of CGP domains for LLD
Both scripts in their work use the pearl barley library
CLI.pm , which allows you to access the Communigate Pro
API .
For the first script, we need one simple ListDomains function, which returns a list of server domains.
An important feature of this script is the need to return the response in JSON-format. I did not want to install a separate JSON library for the pearl (JSON :: DWIW, for example), so as not to produce additional dependencies. I decided that in this case, Perl could do well.
The main features of the script are presented in its help:
discovery_cgp_domains [-h hostname] [-p port] -u username -w password discovery_cgp_domains -h|--help -h hostname - address of DNS name of the server (Default: localhost) -p port - port for connection (Default: 106) -u username - account on CGatePro with grant 'Can Modify All Domains and Accounts Settings' -w password - user password --help - print this help --debug - show debug lines
The source code of the script can be seen
here .
Usage example:
torwald@torwald-station:~/cgp_utility$ ./discovery_cgp_domains.pl -h cgp.server.org -u cgpmonitor -w password { "data":[ {"{#CGPDOMAIN}":"main.domain.org"}, {"{#CGPDOMAIN}":"support.domain.org"}, {"{#CGPDOMAIN}":"hosting.domain.org"} ] }
As we can see, the answer contains a list of objects with one attribute - "#CGPDOMAIN", each of which represents one domain on the server. The attribute name will be used later on when configuring the LLD.
2. Script to get the number of accounts for the domain.
In the second script we add another function - ListAccounts (domain). As the name implies, it returns a list of domain users. There is no separate function to get the number of users in the CGP API, so I’m just using the count of the length of the user list.
In addition, the script can perform the summation of the number of all users on the server. To do this, call it with the -d TOTAL option.
All script options are also presented in his help:
count_cgp_account [-h hostname] [-p port] -u username -w password [-d domain] count_cgp_account -h|--help -h hostname - address of DNS name of the server (Default: localhost) -p port - port for connection (Default: 106) -u username - account on CGatePro with grant 'Can Modify All Domains and Accounts Settings' -w password - user password -d domain - domain name to count accounts; if you use -d TOTAL then you'll get number of all accounts on server --help - print this help --debug - show debug lines
The script simply displays the number of users on the server. Work example:
The source code is
here .
3. We place scripts on the server
In this case, everything is quite simple:
- We place the utility in / usr / local / bin (you can “cut off” the extension) and give the zabbix agent user rights (zabbix by default) to run this utility.
- We place the CLI.pm module where it will be “visible” to the Perl interpreter, launched on behalf of zabbix. I have this / usr / local / lib / perl5.
- In the CGP admin interface, we create a user (for example, cgpmon) with the “Can Modify All Domains and Accounts Settings” rights.
- We check connection from under the user of zabbix.
4. Configure Zabbix Agent
1. Add the following settings to the agent configuration file.
More information about the syntax of working with this parameter and the "asterisk", in particular, can be found in the
documentation .
Thus, now we can get a list of domains and the number of accounts in this domain using the monitoring agent.
2. Restart the agent for the new settings to take effect.
5. Setting up a Zabbix template
For the monitoring of accounts, I created a separate template - Template Communigate Domains Info.
Group of elements
First of all, you should create a Data Item Group in the template. In my case, this is CGP.DomainsInfo. This will allow all data items generated after LLD to be collected in one place.
Detection rule
Open the template and go to the Discovery Rules, where we create a new rule:
Notice the Macro box. In it we write the name of the attribute "{#CGPDOMAIN}". As we remember, it is contained in all objects returned by the script to get a list of domains. And the value of this attribute will be used by all our prototypes (data, triggers and graphs). Often perform LLD for servers is not worth it, once a day is enough. In my case, they could have been put on once a month.
Prototype data item
After these preparations, go to the Prototypes of the data elements and create a new prototype:
Here you should also pay attention to the use of the string "{#CGPDOMAIN}" in the Name and Key fields. When generating new data items, this string will be replaced with the corresponding domain name. Also, do not forget to specify the created Group of elements, otherwise we risk getting a large list of “homeless” data elements.
If desired, you can register prototypes of triggers. For example, trigger create and delete accounts.
Rule for total users
After creating all the prototypes in the template, add a rule to monitor the total number of accounts on the server:
Template for hurry
For hurrying ready template can be found
here .
6. Add nodes to the template
Here, in fact, nothing complicated or RTFM. After adding we wait for the LLD to work, and we get the monitoring of the number of users.
Afterword
Why this monitoring is needed, I, as I said, do not know. For now. I hope smart people will tell me what you can do with this data.
In addition, such an implementation is not safe in principle, since we must keep the Communigate account password with good permissions in the config file. But in principle, this can be circumvented.
Almost the same information can also be obtained using the OS commands.
For example, you can get a list of domains by simply scanning the Domains directory in the Communigate directory.
And the number of users - recursively calculate the number of directories of the form username.macnt within the domain.
But in this case, I wanted to show the general principle of how to automatically collect any information from Communigate using Zabbix. And how to get the necessary data in different ways.