diff --git a/stack/monitor/buildout.cfg b/stack/monitor/buildout.cfg index 61e712f05186880cd236d61b152a1b529a1451bc..ddd04d8677ee16dfcb9ea5c9e8540863749fbe6d 100644 --- a/stack/monitor/buildout.cfg +++ b/stack/monitor/buildout.cfg @@ -153,6 +153,14 @@ destination = ${buildout:parts-directory}/monitor-template-rss-bin filename = status2rss.py mode = 0644 +[mysql-querydigest] +recipe = hexagonit.recipe.download +url = ${:_profile_base_location_}/${:filename} +download-only = true +#md5sum = +filename = mysql-querydigest.py.in +mode = 0644 + [run-apachedex] recipe = hexagonit.recipe.download url = ${:_profile_base_location_}/${:filename} diff --git a/stack/monitor/monitor.cfg.in b/stack/monitor/monitor.cfg.in index 587ece2ec7f6de5d18c193a6403c8f140370076d..0abe8192372bb6a9122686c6ffc5c868fae55b1a 100644 --- a/stack/monitor/monitor.cfg.in +++ b/stack/monitor/monitor.cfg.in @@ -55,6 +55,7 @@ apachedex-result = $${:srv}/apachedex private-directory = $${:srv}/monitor-private log-rss-directory = $${:srv}/ERRORLogAsRSS +mysql-slowquery-report = $${:srv}/mysql-slowquery-report [public-symlink] recipe = cns.recipe.symlink @@ -301,6 +302,28 @@ name = $${apachedex-entries:script-name} frequency = 0 3 * * * command = $${apachedex-entries:rendered} +# Generate Mysql slowqueries report with pt-querry-digest +[mysql-slowquery-report] +recipe = slapos.recipe.template:jinja2 +template = ${mysql-querydigest:location}/${mysql-querydigest:filename} +rendered = $${cron-mysql-slowquery-report:command} +mode = 0700 +extensions = jinja2.ext.do +output-folder = $${monitor-directory:mysql-slowquery-report} +extra-context = +context = + raw python_executable ${buildout:executable} + raw pt_query_executable ${perl:siteprefix}/bin/pt-query-digest + key output_folder :output-folder + $${:extra-context} + +[cron-mysql-slowquery-report] +<= cron +recipe = slapos.cookbook:cron.d +name = mysql-slowquery-report +frequency = 0 3 * * * +command = $${monitor-directory:bin}/mysql_slowquery_report.py + [monitor-instance-log-access] recipe = plone.recipe.command command = if [ -d $${:source} ]; then ln -s $${:source} $${monitor-directory:private-directory}/instance-logs; fi diff --git a/stack/monitor/mysql-querydigest.py.in b/stack/monitor/mysql-querydigest.py.in new file mode 100644 index 0000000000000000000000000000000000000000..f4e4a340335cea16f20882e05a8ac58bb5ef5604 --- /dev/null +++ b/stack/monitor/mysql-querydigest.py.in @@ -0,0 +1,53 @@ +#!{{ python_executable }} +# BEWARE: This file is operated by slapgrid +# BEWARE: It will be overwritten automatically + +import os, errno +import subprocess +from datetime import date +import gzip + +slow_query_path = "{{ slow_query_path }}".strip() +output_folder = "{{ output_folder }}".strip() + +if not slow_query_path: + exit(0) + +#pt-query-digest --processlist h=host,user=user,password=pass --output=slow.log +today = date.today().strftime("%Y-%m-%d") +folder_month = os.path.join(output_folder, 'Mysql-SlowQuery-Digest-%s' % + date.today().strftime("%Y-%m")) +out_file = os.path.join(folder_month, 'slow_query_report_%s.log' % today) +slow_log = slow_query_path % {'date': date.today().strftime("%Y%m%d")} +delete = False + +if not os.path.exists(slow_log) or not os.path.isfile(slow_log): + print "ERROR: cannot read mysql slow query log file '%s'. Exiting..." % slow_log + exit(1) + +try: + os.makedirs(folder_month) +except OSError as exc: + if exc.errno == errno.EEXIST and os.path.isdir(folder_month): + pass + else: raise + +if slow_log.endswith('.gz'): + slow_query_log = os.path.join(folder_month, 'slow_%s.log' % today) + delete = True + with open(slow_query_log, 'w') as slowfile: + f = gzip.open(slow_log, 'rb') + slowfile.write(f.read()) + f.close() +else: + slow_query_log = slow_log + +pt_query_digest = "{{ pt_query_executable }}".strip() +argument_list = [pt_query_digest, slow_query_log] +output = open(out_file, 'w') + +subprocess.check_call(argument_list, stdout=output) +output.close() +if delete: + os.unlink(slow_query_log) +