Remove all comments except the lasts

Hi guys!
Please help me figured out. I use Icinga API and python for removing comments. I want to delete all comments except for example the last 5.
Or in this case, I have to use python way?
Thanks!

HI Akontiy,

Welcome to the forum.

On your master host you can type the following:

icinga2 object list --type Comment

This will list all your current comments it should look roughly like this:

Object 's9d149' of type 'Comment':
  % declared in '.conf', lines 1:0-1:68
  * __name = ""
  * author = "w"
    % = modified in '.conf', lines 2:2-2:20
  * entry_time = 1600960065.904446
    % = modified in '.conf', lines 3:2-3:31
  * entry_type = 4
    % = modified in '.conf', lines 4:2-4:22
  * expire_time = 0
    % = modified in '.conf', lines 5:2-5:23
  * host_name = ""
    % = modified in '.conf', lines 6:2-6:42
  * name = "5503989d-e060-4556-b057-26942249d149"
  * package = "_api"
  * persistent = true
    % = modified in '.conf', lines 7:2-7:18
  * service_name = "yum-updates"
    % = modified in '.conf', lines 8:2-8:29
  * source_location
    * first_column = 0
    * first_line = 1
    * last_column = 68
    * last_line = 1
    * path = ".conf"
  * templates = [ "5503989d-e060-4556-b057-26942249d149" ]
    % = modified in '.conf', lines 1:0-1:68
  * text = "We know"
    % = modified in '.conf', lines 9:2-9:17
  * type = "Comment"
  * zone = "AW-US"
    % = modified in '.conf', lines 11:2-11:15

What you are probably looking for is the:

entry_time = 1600960065.904446

This is Linux time in epoch, you can convert them here for example:

More info:

So now the python part :slight_smile:
you can probably get away with using requests in python in bash you would do this:

curl -k -s -u user:password 'https://localhost:5665/v1/objects/comments' 

Documentation can be found here:

Python requests:

With that in return you are getting JSON that you can filter to get a dictionary or a list with all down time ID`s and their time stamp, and sort them and delete them but the last 5 anyway lots of scripting ahead of you!

A simple json example:

{
	"results": [{
		"attrs": {
			"__name": "",
			"active": true,
			"author": "",
			"entry_time": 1603468380.399639,
			"name": "f002dddb-143b-4706-9576-bbff8d9735f8",

you would want the name and the entry_time to filter and delete the ones you no longer need

Hi William van Beek!
Thanks for the answer!
I almost made a script, yeah its a lot)
One more question.
When I make a request https://localhost:5665/v1/objects/comments
I get all comments without separating host and service.
Is there a way how to get only for host or service?
And yes I tried to do like this https://localhost:5665/v1/objects/comments?joins=host

Yes that should be doable as you mentioned

A example from the documentation website:

In case you want to fetch all comments for hosts and services, you can use the following query URL (similar example for downtimes):
https://localhost:5665/v1/objects/comments?joins=host&joins=service

more about that here:
https://icinga.com/docs/icinga2/latest/doc/12-icinga2-api/#object-query-joins

It will get complicated quickly but another example you can find on that site that is probably what you want to look at is this:

curl -k -s -u root:icinga -H 'Accept: application/json' \
 -H 'X-HTTP-Method-Override: GET' -X POST \
 'https://localhost:5665/v1/objects/comments' \
 -d '{ "joins": [ "service.name", "service.acknowledgement", "service.acknowledgement_expiry" ], "attrs": [ "author", "text" ], "filter": "service.acknowledgement!=0 && service.acknowledgement_expiry==0", "pretty": true }'

adjust the -d according to your requirements