Example of using ispmanager API in plugin: output of a database list
Below is an example of a plugin that displays in a static text field a list of databases that a given ispmanager user has access to.
This example assumes that you are logged in to ispmanager as Superadministrator (root) and are using ispmanager lite/pro/host.
- Create a file with plugin XML-description.
- Go to /usr/local/mgr5/etc/xml directory and create an
ispmgr_mod_dblist.xml
file in it. - Open this file and copy the following code into it:
<?xml version="1.0" encoding="UTF-8"?>
<mgrdata>
<mainmenu level="30">
<modernmenu>
<node type="noname" name="dblist_group">
<node name="dblist" />
</node>
</modernmenu>
</mainmenu>
<handler name="dblist.sh" type="xml">
<func name="dblist"/>
</handler>
<lang name="ru">
<messages name="desktop">
<msg name="modernmenu_dblist">Database list</msg>
</messages>
</lang>
</mgrdata>
4. Create a handler: go to the /usr/local/mgr5/addon
directory and create the dblist.sh
file there. The handler file must be executable for the root user and group, so give the file permissions 750 (chmod 750 /usr/local/mgr5/addon/dblist.sh
). Open the file for editing and copy the code below into it:
#!/bin/bash
dblines=$(/usr/local/mgr5/sbin/mgrctl -m ispmgr db su=${AUTH_USER})
dbshtml=$(sed -rn "s/.*pair\=[a-zA-Z_]+->([^ ]+).*name\=([a-zA-Z_]+).*\
/<b>\2<\/b> (\1)<br>/p" <<< "$dblines")
cat << EOM
<?xml version="1.0" encoding="UTF-8"?>
<doc lang="ru" func="database_list">
<metadata type="form">
<form>
<field name="dblist_field">
<textdata name="dblist" type="msg" />
</field>
</form>
</metadata>
<messages>
<msg name="title">Database list</msg>
<msg name="dblist"><![CDATA[${dbshtml}]]></msg>
</messages>
</doc>
EOM
Explanation of the above code:
- First, the result of mgrctl execution is written to the variable dblines (for more info, see Mgrctl utility) for db function (ispmanager built-in function that returns a list of databases), and the function is executed for the user who opened the page - parameter su=${AUTH_USER} of mgrctl utility.
- The strings that end up in the dblines variable are then processed using sed to format the database name (in bold) and server type (e.g., mysql) in HTML brackets with <br> appended at the end of each line.
- The XML forms are then rendered with a single field, static text, whose value is the HTML obtained in the previous step. Note that to avoid conflicts between XML and HTML tags, the HTML is placed inside the CDATA section.
You can read more about the structure of the XML returned by handlers in Structure and functionality of plugins.
An example on the same topic in PHP, Python and Node.js can be found in Examples of plugins in PHP, Python, Node.js