官术网_书友最值得收藏!

  • Extending SaltStack
  • Joseph Hall
  • 377字
  • 2021-07-16 12:42:20

Troubleshooting SDB modules

We've already covered some error handling that can be added to our SDB modules, but you may still encounter problems. Like grains and pillars, the most common involve data not showing up when expected.

SDB data not showing up

You may find that when you include an SDB URI in your configuration, it doesn't resolve as you think it might. If you've made typos in the earlier SDB code, you have probably already figured out that sdb.get is more than happy to raise trace backs when there are syntactical errors. But if using salt-call on sdb.get doesn't raise any errors that you can see, then it may not be a problem in your code.

Before you start to blame other services, it's best to make sure that you're not to blame. Start logging key pieces of information, to make sure it's showing up as you expect. Make sure to add the following lines toward the top of your module:

import logging
log = logging.getLogger(__name__)

Then you can use log.debug() to log those pieces of information. If you're logging sensitive pieces of information, you may want to use log.trace() instead, just in case you forget to take the log messages out.

You may want to start with logging the information coming into each function, to make sure it looks like you expect. Let's go ahead and take a look at our get() example from earlier, with some logging added in:

def get(key, profile=None):
    '''
    Get a value from a JSON file
    '''
    import pprint
    log.debug(key)
    log.debug(pprint.pformat(profile))
    with salt.utils.fopen(profile['json_file'], 'r') as fp_:
        json_data = json.load(fp_)
    return json_data.get(key, None)

We've only added a couple of log lines here, but we used Python's pprint library to format one of them. The pprint.pformat() function formats text that is meant to be stored in a string or passed to a function, instead of just dumping it to STDOUT like pprint.pprint() does.

If your SDB module connects to a service, you may discover that the service itself is unavailable. This may be due to unknown or unintended firewall rules, a network error, or actual downtime on the service itself. Scattering log messages throughout your code will help you discover where it is falling down, so that you can address it there.

主站蜘蛛池模板: 绥德县| 永泰县| 英吉沙县| 蛟河市| 泰和县| 宣化县| 许昌县| 孙吴县| 昭觉县| 鸡西市| 鹿邑县| 都匀市| 万荣县| 方正县| 焉耆| 阳城县| 荣昌县| 汕尾市| 卓资县| 堆龙德庆县| 南靖县| 稷山县| 武山县| 揭阳市| 施甸县| 吉林省| 东阳市| 长乐市| 福安市| 永清县| 罗山县| 浑源县| 吴堡县| 赤水市| 蓬莱市| 新巴尔虎左旗| 抚松县| 新闻| 海丰县| 都兰县| 开封县|