You are here

"Bulk delete" of hosts using the API.

13 posts / 0 new
Last post
Carlos Parada
carlos.parada@paddypowerbetfair.com's picture
"Bulk delete" of hosts using the API.

Hi guys,

Which would be the best way of automating the deletion of about ~50 hosts, using the API?

Kind regards.

 

Duncan Ferguson
dferguson's picture
Thre are details on https:/

Thre are details on https://knowledge.opsview.com/articles/opsview-monitor-510/5-rest-api.html for various methods of talking to the Rest API, such as coding in Perl, or using Bash with curl or wget

You would need to the the config URI's, as documented on https://knowledge.opsview.com/articles/opsview-monitor-510/53-rest-api-config.html

I would suggest you first fetch all the hosts matching the criteria you need before you then delete them.  It would also be a good idea to run a reload in the UI first to ensure all uncommitted changes are in place, which will also create a restore point (via 'menu -> help -> audit log') for you should any problems arise.

Using the opsview_rest command, to search for and then delete all hosts that have names starting with 'host' (e.g. host1, host2, host3), you could:

$ opsview_rest --username=XXXX--password=YYYY --pretty GET config/host?json_filter='{"name":{"-like":"host%25"}}'

where the %25 is the URL encoded value for a %, the SQL wildcard character.

You can then parse the JSON output, check the 'name' fields of each stanza to confirm the correct hosts are identified, and then for each one, using the 'id' field, delete them:

$ opsview_rest --username=XXXX--password=YYYY --pretty DELETE config/host/5

  Duncs

Carlos Parada
carlos.parada@paddypowerbetfair.com's picture
Hi,

Hi,

Thanks for your reply.

I am getting the below error when trying to run that.

 

{
  detail  => "malformed JSON string, neither tag, array, object, number, string or atom, at character offset 0 (before \"%7B%22name%22:%7B%22...\") at (eval 363) line 161.\n",
  message => "Error when parsing json_filter",
}
 

Kind regards.

 

Duncan Ferguson
dferguson's picture
Can you post the part of the

Can you post the part of the URI including the json filter you are using?

  Duncs

Carlos Parada
carlos.parada@paddypowerbetfair.com's picture
Hi Duncs,

Hi Duncs,

I am using the below command.

#opsview_rest --username admin --password=password --pretty GET config/host?json_filter='{"name":{"-like":"prdsbx%25"}}'

 

Thanks.

Duncan Ferguson
dferguson's picture
I coipied and pasted that

I coipied and pasted that command onto my test system and it worked as expected

$ opsview_rest --username=XXXX--password=YYYY--pretty GET config/host?json_filter='{"name":{"-like":"prdsbx%25"}}'
{
  list => [],
  summary => { allrows => 4, page => 1, rows => 0, totalpages => 1, totalrows => 0 },
}
$

I can only guess that when you copied and pasted the line into this web site the quote characters were automatically changed to single and double quotes, or similar.

  Duncs

Carlos Parada
carlos.parada@paddypowerbetfair.com's picture
Yeah, it's weird, because I

Yeah, it's weird, because I also tried just typing the full command.

 

{'detail' => 'malformed JSON string, neither tag, array, object, number, string or atom, at character offset 0 (before "%7B%22name%22:%7B%22...") at (eval 363) line 161.
','message' => 'Error when parsing json_filter'}
Error GETing https://xxx.xx.xx/rest/config/host?json_filter=%257B%2522name%2522:%257B... Bad Request at /opsviewha/nagios/bin/../perl/lib/WWW/Mechanize.pm line 2833.

 

Duncan Ferguson
dferguson's picture
Are you running the commands

Are you running the commands as the nagios user or the root user?  I ask as I noticed a command prompt on your earlier reply was '#'

I have run my commands as nagios.

  Duncs

Carlos Parada
carlos.parada@paddypowerbetfair.com's picture
Could it be because of the

Could it be because of the perl version I am running?

[root@server ~]# rpm -q perl
perl-5.10.1-136.el6_6.1.x86_64
[root@server ~]# perl -v

This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi

 

Duncan Ferguson
dferguson's picture
No, I don't believe it is due

No, I don't believe it is due to the version of perl - I think it more likely it is due to your locale and keyboard layout.

Can you download the attached onto your server and just amend the username and password fields in it before running?  Since I have created this on my system (where it worked) this would help rule out if it is something around your terminal and keyboard input.

opsview_restapi_test.

Do you have https enabled on your opsview instance and are you using apache as the front end proxy?

If this doesnt work, the next step would be to put opsview-web into debug mode and watch what is happening - what version of Opsview are you running?

Carlos Parada
carlos.parada@paddypowerbetfair.com's picture
I ran the commands as the

I ran the commands as the nagios user on the master server, just entered the hash by hand before.

Carlos Parada
carlos.parada@paddypowerbetfair.com's picture
Hi,

Hi,

The script fails with the same error.

 

-bash-4.1$ sh test.sh
{
  detail  => "malformed JSON string, neither tag, array, object, number, string or atom, at character offset 0 (before \"%7B%22name%22:%7B%22...\") at (eval 363) line 161.\n",
  message => "Error when parsing json_filter",

 

Yeah, https is enabled and, actually, I always get the below error when logging in as nagios for working with the API.

Can't connect to XXXXXXX:443 (certificate verify failed)

LWP::Protocol::https::Socket: SSL connect attempt failed error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed at /mnt/opsviewha/nagios/bin/../perl/lib/LWP/Protocol/http.pm line 49.

Error GETing XXXXXXXX/rest: Can't connect to XXXXXXXX:443 (certificate verify failed) at /mnt/opsviewha/nagios/bin/../perl/lib/WWW/Mechanize.pm line 2833.

 

...which Opsview Support recommended fixing by running:

export PERL_LWP_SSL_VERIFY_HOSTNAME=0

 

 

That's right, we use Apache as frontend and our Opsview version is 4.6.3

Thanks.

Duncan Ferguson
dferguson's picture
This issue was resolved in a

This issue was resolved in a customer support ticket by using the following:

opsview_rest --username=XXXX --password=YYYY --data-format=json --pretty GET config/host"?&s.hostgroup.name=HOSTGROUP&rows=all"

Duncs