Skip to content

Commit 33539cc

Browse files
committed
doc: added Zabbix hosts mass rename examples
1 parent fe2a09b commit 33539cc

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed

README.md

+147
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,153 @@ Or through the Zabbix web interface:
359359
re_send = False
360360
```
361361
362+
**Major update**
363+
If you want to upgrade mamonsu to a version that is not compatible with the previous one, what you must do to continue using the application depends on whether you need to retain the metrics data collected.
364+
If you do not need to retain the collected data, just unlink old template and link a new one.
365+
366+
If you need to retain the collected data, do the following:
367+
368+
1. Install the new version of mamonsu.
369+
2. Generate a new template for the Zabbix server.
370+
3. If you performed a bootstrap using the previous version of mamonsu, run the bootstrap command again.
371+
4. Upload the new template to the Zabbix server.
372+
5. Rename the host for which you want to retain the collected data and leave the old template linked to that host.
373+
6. Create a new host for the same system and link the new template to it.
374+
7. Restart mamonsu. It will collect data for the new host. The old host will no longer be used, but the data collected will be available.
375+
376+
The difficulty is that Zabbix cannot massively rename nodes.
377+
We offer the following recommendations:
378+
1. If you have access to the Zabbix database:
379+
Mass rename hosts via SQL:
380+
```shell
381+
zabbix=# SELECT host, name FROM hosts
382+
zabbix-# WHERE host LIKE '%local-pg%';
383+
-[ RECORD 1 ]----
384+
host | local-pg-2
385+
name | local-pg-2
386+
-[ RECORD 2 ]----
387+
host | local-pg-3
388+
name | local-pg-3
389+
390+
zabbix=# UPDATE hosts
391+
zabbix=# SET host = host || ' OLD-MAMONSU',
392+
zabbix=# name = name || ' OLD-MAMONSU'
393+
zabbix=# WHERE host LIKE '%local-pg%';
394+
UPDATE 2
395+
zabbix=# SELECT host, name FROM hosts
396+
zabbix=# WHERE host LIKE '%local-pg%';
397+
-[ RECORD 1 ]----------------
398+
host | local-pg-2 OLD-MAMONSU
399+
name | local-pg-2 OLD-MAMONSU
400+
-[ RECORD 2 ]----------------
401+
host | local-pg-3 OLD-MAMONSU
402+
name | local-pg-3 OLD-MAMONSU
403+
```
404+
2. Using Zabbix API:
405+
API query:
406+
```shell
407+
curl -H "Content-type: application/json-rpc" -X POST http://zabbix/api_jsonrpc.php -d'
408+
{
409+
"jsonrpc": "2.0",
410+
"method": "host.update",
411+
"params": {
412+
"hostid": "HOST_ID",
413+
"host": "local-pg-3 OLD-MAMONSU",
414+
"name": "local-pg-3 OLD-MAMONSU"
415+
},
416+
"auth": "AUTH_TOKEN",
417+
"id": 1
418+
}'
419+
```
420+
<details>
421+
<summary>Script</summary>
422+
423+
```shell
424+
#!/bin/bash
425+
426+
ZABBIX_URL="http://zabbix/"
427+
ZABBIX_USER="Admin"
428+
ZABBIX_PASSWORD="zabbix"
429+
ZABBIX_PATTERN=""
430+
ZABBIX_SUFFIX="OLD"
431+
432+
for parameter in "$@"
433+
do
434+
case $parameter in
435+
-u=*|--url=*) # zabbix url
436+
ZABBIX_URL="${parameter#*=}"
437+
shift
438+
;;
439+
-U=*|--user=*) # zabbix user
440+
ZABBIX_USER="${parameter#*=}"
441+
shift
442+
;;
443+
-P=*|--password=*) # zabbix password
444+
ZABBIX_PASSWORD="${parameter#*=}"
445+
shift
446+
;;
447+
-p=*|--pattern=*) # zabbix host pattern
448+
ZABBIX_PATTERN="${parameter#*=}"
449+
shift
450+
;;
451+
-s=*|--suffix=*) # zabbix host suffix
452+
ZABBIX_SUFFIX="${parameter#*=}"
453+
shift
454+
;;
455+
*)
456+
# unknown option
457+
;;
458+
esac
459+
done
460+
461+
# get zabbix auth token
462+
auth_token=$(curl -H "Content-type: application/json-rpc" -X POST ${ZABBIX_URL}api_jsonrpc.php -d'
463+
{
464+
"jsonrpc": "2.0",
465+
"method": "user.login",
466+
"params": {
467+
"user": "'${ZABBIX_USER}'",
468+
"password": "'${ZABBIX_PASSWORD}'"
469+
},
470+
"id": 1
471+
}' | python3 -c "import sys, json; print(json.load(sys.stdin)['result'])")
472+
473+
# get array of zabbix hosts to rename
474+
readarray -t hosts < <(mamonsu zabbix --url=${ZABBIX_URL} --user=${ZABBIX_USER} --password=${ZABBIX_PASSWORD} host list | awk '{ print "\""$0"\""}' | grep ${ZABBIX_PATTERN})
475+
hosts=("${hosts[@]//\"/}")
476+
477+
hosts_dict={}
478+
# create dict from array (id:name)
479+
for host in "${hosts[@]}"
480+
do
481+
hosts_dict[$(mamonsu zabbix --url=${ZABBIX_URL} --user=${ZABBIX_USER} --password=${ZABBIX_PASSWORD} host id "${host}")]=$host
482+
done
483+
484+
for key in "${!hosts_dict[@]}"; do
485+
if [ ${key} -ne 0 ]; then
486+
eval 'curl -H "Content-type: application/json-rpc" -X POST ${ZABBIX_URL}api_jsonrpc.php -d '\''
487+
{
488+
"jsonrpc": "2.0",
489+
"method": "host.update",
490+
"params": {
491+
"hostid": "'${key}'",
492+
"host": "'${hosts_dict[$key]}' '${ZABBIX_SUFFIX}'",
493+
"name": "'${hosts_dict[$key]}' '${ZABBIX_SUFFIX}'"
494+
},
495+
"auth": "'${auth_token}'",
496+
"id": 1
497+
}'\'''
498+
fi
499+
done
500+
```
501+
502+
</details>
503+
504+
Script usage example:
505+
```shell
506+
./rename_zabbix_hosts.sh --url=http://localzabbix/ --pattern="local-pg" --suffix="OLD-MAMONSU"
507+
```
508+
362509
## Additional chapters
363510
- [**Adding custom plugins**](documentation/adding_custom_plugins.md)
364511
- [**Configuration file**](documentation/configuration_file.md)

0 commit comments

Comments
 (0)