Forum Discussion

19 Replies

  • On 2/26/2021 at 6:33 PM, Stuart Weenig said:

    Looks like the entire response has a .to_dict() method. However, this results in a dictionary where one of the entries is called "items". Unfortunately, dictionaries have a native method called items(), so this conflicts and you can't actually access that key pythonically (thanks Swagger). The above method is what I'd use.

    Thank you, I'll give that a try. 

  • On 2/26/2021 at 6:33 PM, Stuart Weenig said:

    Looks like the entire response has a .to_dict() method. However, this results in a dictionary where one of the entries is called "items". Unfortunately, dictionaries have a native method called items(), so this conflicts and you can't actually access that key pythonically (thanks Swagger). The above method is what I'd use.

    Actually there was some weird behaviour going on when I was trying to access the data and was scratching my head but makes sense now. Thanks

  • Does anyone have an example for accessing the custom columns? I cannot figure it out.

  • Anonymous's avatar
    Anonymous

    What happens if you do this?

    for alert in lm.get_alert_list().items:
        print(alert.custom_columns)

    If you get a bunch of "none"s, then it may be that it's not fetched as part of the method. I sort of remember there being something special you had to do to get the custom columns, but I can't remember what it was. Try the above and let me know if all you get are "none"s.

  • def get_devicealert():
        value_list = []
        cursor = conn.cursor()
        #devicealert = chunker(api_instance.get_alert_list,'needMessage:"true", cleared:"*"')
        devicealert = chunker(api_instance.get_alert_list,'custom_Columns:"snow_company_sys_id"')
        for i in devicealert:
            for c in i.items:
                print(c.custom_columns)
            query = """INSERT INTO LMDeviceAlert(Internal_ID, chain,id, Monitor_Object_ID, Monitor_Oobject_Name, Monitor_Object_Type, [Rule], Rule_ID, Severity, Start_Epoch, End_Epoch, Acked_Epoch, Alert_Value, Ack_Comment, Acked, Instance_ID, Instance_Name, SDT, Detail_Message)
            values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"""
            value = (
                i.internal_id,
                i.chain,
                i.id,
                i.monitor_object_id,
                i.monitor_object_name,
                i.monitor_object_type,
                i.rule,
                i.rule_id,
                #The alert severity, where 2=warning, 3=error and 4=critical
                i.severity,
                time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(i.start_epoch)),
                time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(i.end_epoch)),
                time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(i.acked_epoch)),
                i.alert_value.replace("'", ""),
                i.ack_comment,
                i.acked,
                i.instance_id,
                i.instance_name,
                str(i.sdt).replace("'", ""),
                str(i.detail_message).replace("'", "")
                )
            value_list.append(value)
        try:
            cursor.executemany(query, value_list)
            conn.commit()
        except:
            print('DevicesAlert\nQuery={}\n'.format(query, value_list))
            logging.exception('DevicesAlert\nQuery={}\n'.format(value))    
        cursor.close() 

    I may have the placement wrong, but the error is "Alert' object has no attribute 'items. Chunker is a function that executes the call until all records are obtained and exists to address the 1k record limit.

  • If I place the sample you provided elsewhere in that function I do get all none values.

  • Anonymous's avatar
    Anonymous
    5 hours ago, DBA-ONE said:
    
    devicealert = chunker(api_instance.get_alert_list,'custom_Columns:"snow_company_sys_id"')
        for i in devicealert:
            for c in i.items:

     

    Haven't used chunker much, but if devicealert contains the response of the api_instance.get_alert_list() method, then you should be looping through devicealert.items. Not looping through device alert looking for items on each child in devicealert.

  • I will change the code to do that, but I still have the issue of the custom columns not being exposed. The docs don't offer any information other than the possibility of accessing them.

  • Anonymous's avatar
    Anonymous

    @Sarah Terry do you know what is required in order to get the custom columns to be returned either in the API or through the Python SDK?