Skip to content
Snippets Groups Projects
save_alert_channels.py 1.88 KiB
Newer Older
ArthurYueh's avatar
ArthurYueh committed
import argparse
from dashboardApi import *
from commons import *
from datetime import datetime

parser = argparse.ArgumentParser()
parser.add_argument('path',  help='folder path to save alert channels')
args = parser.parse_args()

folder_path = args.path
log_file = 'alert_channels_{0}.txt'.format(datetime.today().strftime('%Y%m%d%H%M'))

def get_all_alert_channels_in_grafana():
    (status, content) = search_alert_channels()
    if status == 200:
        channels = content
        print("There are {0} channels:".format(len(channels)))
        for channel in channels:
            print("name: {}".format(to_python2_and_3_compatible_string(channel['name'])))
        return channels
    else:
        print("query alert channels failed, status: {}, msg: {}".format(status, content))
        return []
    
def save_alert_channel(channel_name, file_name, alert_channel_setting):
    file_path = folder_path + '/' + str(file_name) + '.alert_channel'
ArthurYueh's avatar
ArthurYueh committed
    with open(file_path, 'w') as f:
        f.write(json.dumps(alert_channel_setting))
    print("alert_channel:{0} is saved to {1}".format(channel_name, file_path))

def get_indivisual_alert_channel_and_save(channels):
    for channel in channels:
        if 'uid' in channel:
            channel_identifier = channel['uid']
        else:
            channel_identifier = channel['id']
            
ArthurYueh's avatar
ArthurYueh committed
        save_alert_channel(
            to_python2_and_3_compatible_string(channel['name']),
            to_python2_and_3_compatible_string(channel_identifier),
ArthurYueh's avatar
ArthurYueh committed
            channel
        )
        file_path = folder_path + '/' + log_file
        with open(u"{0}".format(file_path) , 'w+') as f:
            f.write('{}\t{}'.format(to_python2_and_3_compatible_string(channel_identifier), to_python2_and_3_compatible_string(channel['name'])))
ArthurYueh's avatar
ArthurYueh committed


alert_channels = get_all_alert_channels_in_grafana()
get_indivisual_alert_channel_and_save(alert_channels)
print_horizontal_line()