Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load dashboards for kibana on startup US314 #116

Merged

Conversation

john-gress
Copy link

Copy link

@sperry94 sperry94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few minor comments, but nothing serious. Did not test.

search_type = "search"
session = requests.Session()
# Retry on Internal Server Error (500) and Service Unavailable (503)
retries = Retry(total=5, backoff_factor=0.3, status_forcelist=[ 500, 503])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to retry on other errors here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added 408, 502, and 504.


session = requests.Session()
# Retry on Internal Server Error (500) and Service Unavailable (503)
retries = Retry(total=5, backoff_factor=0.3, status_forcelist=[ 500, 503])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to retry on other errors here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added:

  • Request Timeout (408)
  • Bad Gateway (502)
  • Gateway timeout (504)

es_id,
content)
try:
url = 'http://localhost:9200/.kibana/_doc/' + es_id

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we might want to grab the id from the imported file? If ever the es_id and the id from the dashboard differ, I'm not sure which id elastic search would take

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The id is not saved in the json, so not available there. I think we are depending on the name of the file to have the correct id.

es_ok = check_elasticsearch_health()
if es_ok:
# Create arrays of the filenames in the resources directory
objects = [filename for filename in listdir(resources) if isfile(join(resources, filename))]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this iteration could get moved into the load_assets function, with the only parameter to load_assets being the path_to_files

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Changed as suggested.

Copy link

@Schatzman Schatzman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments but just about python style, nothing that NEEDS to change 👍 - Reviewed but not tested.


session = requests.Session()
# Retry on Internal Server Error (500) and Service Unavailable (503)
retries = Retry(total=5, backoff_factor=0.3, status_forcelist=[ 500, 503])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to retry for 502 (bad gateway) and 504 (gateway timeout) also potentially? https://httpstatuses.com/

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

return False

def get_search_title_json(es_type, es_title):
esField = es_type + '.title'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guido Van Rossum frowns upon your camel case, python PEP8 prefers snake case for functions and variables http://www.python.org/dev/peps/pep-0008/

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guido is right. Changed to snake case.

session.mount('https://', HTTPAdapter(max_retries=retries))
session.mount('http://', HTTPAdapter(max_retries=retries))
esQuery = json.dumps(es_query)
headers = { 'Content-type': 'application/json' }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a lot of variability in this file between single and double quotes, might be good to pick one or the other and stick with them throughout for consistency. I tend to default to single quotes in python.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Single quotes it is. Fixed.

@@ -118,33 +162,40 @@ def main(argv):
global OUTPUT_DIR
global FILE_PATHS_AND_CONTENTS
global TYPE
global ID
global TITLE

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of these globals are not "pythonic". Prefer passing as params and returning modified variables rather than modifying globals.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

es_ok = check_elasticsearch_health()
if es_ok:
# Create arrays of the filenames in the resources directory
objects = [filename for filename in listdir(resources) if isfile(join(resources, filename))]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh fancy list comprehension! ;)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kudos to @craig-cogdill

else:
print "ERROR: Did not find any " + TYPE + " named " + ID + " in Elasticsearch."
sys.exit(2)
print "elasticsearch is not ready\n"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be logger.error()?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. Again this lets the user know their request didn't work.

get_all_dashboard_content_from_ES(dashboard_raw=UTIL.safe_list_read(list_ob=FILE_PATHS_AND_CONTENTS, key=get_full_path(asset_id=asset_id)))
export_all_files()
else:
print "ERROR: Did not find any " + TYPE + " named " + TITLE + " in Elasticsearch."

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be logger.error()?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. In this case, the error has already been logged to file in the call to get_asset. This output to the terminal lets the user know it failed. Added additional print statement to point the user to the log file for more info.


session = requests.Session()
# Retry on Internal Server Error (500) and Service Unavailable (503)
retries = Retry(total=5, backoff_factor=0.3, status_forcelist=[ 500, 503])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space between [ 500,

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Also added 408, 502, and 504.

return True

except Exception as err:
logger.warning("[loadAssets.py] Caught HTTP exception: {0}".format(err))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[loadAssets.py] --> [exportAssets.py]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

return True, response.text

except Exception as err:
logger.info("[getEs.py] Caught HTTP exception: {0}".format(err))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[getEs.py] --> [exportAssets.py]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

if not response.ok:
response.raise_for_status()

logger.info("GET RETURNS: " + "\n" + UTIL.pretty_format(response.json()))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could probably go before if not response.ok so that we see an error response as well? Or we could remove it entirely.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved before if not response.ok

success, ret = get_asset(INDEX, panel_type, panel_id)
es_query = get_search_id_json(panel_type, panel_id)
success, asset_raw = get_asset(es_query)
asset_id = panel_type + ":" + panel_id

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should go in the if block below

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Fixed.

load_assets(resources, objects)
set_default_index_pattern()
else:
print "elasticsearch is not ready\n"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logger.error()?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. In this case it makes sense because the script is run out of a systemd unit file.

@john-gress john-gress merged commit a42da03 into logrhythm:7.2.0_NM Sep 20, 2019
sperry94 pushed a commit that referenced this pull request Jan 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants