mclarkson / nagrestconf

Nagios REST Interface
12 stars 5 forks source link

Ability to add, Inability to delete host with special character in the name field #28

Closed cullenp closed 9 years ago

cullenp commented 9 years ago

I used the following command to add a host to nagrestconf.

curl -v -knX POST -d "json={\"folder\":\"local\", \"name\":\"RS-ib1.prod_#220\", \"alias\":\"IB Production IB1\", \"ipaddress\":\"$HOST_IP\", \"template\":\"windows-server\", \"hostgroup\":\"IB\", \"servicesets\":\"windows-server\"}" http://$NAGIOSIP/rest/add/hosts

Our hosting service adds a number including a # to the name. This completed with a "Success" though the instance is not in the correct hostgroup or service set, nor can I edit it with the web GUI. Any attempt to edit or delete shows the incomplete name as RS-ib1.prod_ and therefore does not reference the host entry. I get an error "NAGCTL ERROR: Item not found".

If I try the delete command:

curl -v -knX POST -d "json={\"folder\":\"local\", \"name\":\"RS-ib1.prod_#220\", \"alias\":\"IB Production IB1\", \"ipaddress\":\"$HOST_IP\", \"template\":\"windows-server\", \"hostgroup\":\"IB\", \"servicesets\":\"windows-server\"}" http://$NAGIOSIP/rest/delete/hosts

I get the following error

"NAGCTL ERROR: Host is referenced by one or more services. Will not delete."

mclarkson commented 9 years ago

Thanks for the detailed report and I can confirm the problem. Unexpected since '#' isn't allowed in a DNS name. Would you be able to use a name without a '#'?

cullenp commented 9 years ago

In our case I used a nickname the cloud management uses and they control the syntax. "Name" in nagios does not typically need to be DNS resolvable. In any case I will use another identifier. My short term problem is how do I remove the existing server using nagrestconf?

mclarkson commented 9 years ago

You are correct, and anyway, nagrestconf should complain instead of adding a value it can't use later on. There are a number of fields that I paid special attention to for allowing special characters marked as "'E' - The field should be URL encoded. " in the reference documentation. The others, like this one, are a bit sensitive.

The files will have to be edited.

# cd to either '/etc/nagios/...' or '/etc/nagios3/...' depending on the platform
cd /etc/nagios/objects/local/setup

# SHOW what the lines will be changed to. No changes will be made.
sed -n 's/\(^.*\)#\(.*[^,].*\)/\1\2/p' local_hosts.setup
sed -n 's/\(^.*\)#\(.*[^,].*\)/\1\2/p' /etc/nagios3/objects/local/setup/local_services.setup

If it looks good, so only the names with hashes in are changed, then make the change using -i (and removing 'p').

# Changes will be made!
sed -i 's/\(^.*\)#\(.*[^,].*\)/\1\2/' local_hosts.setup
sed -i 's/\(^.*\)#\(.*[^,].*\)/\1\2/' /etc/nagios3/objects/local/setup/local_services.setup

I just used the above sed commands and pasted them having used your previous commands to break nagrestconf, so it should work otherwise use vim (but then you really should lock the directory I suppose).

cullenp commented 9 years ago

Stellar. Thank you so much. Worked like a champ. I owe you a beer.

Cullen

mclarkson commented 9 years ago

No problem :-)