[slap-parameters]
recipe = slapos.cookbook:slapconfiguration
computer = $${slap-connection:computer-id}
partition = $${slap-connection:partition-id}
url = $${slap-connection:server-url}
key = $${slap-connection:key-file}
cert = $${slap-connection:cert-file}

[monitor-parameters]
json-filename = monitor.json
json-path = $${monitor-directory:monitor-result}/$${:json-filename}
rss-filename = rssfeed.html
rss-path = $${monitor-directory:public-cgi}/$${:rss-filename}
executable = $${monitor-directory:bin}/monitor.py
port = 9685
htaccess-file = $${monitor-directory:etc}/.htaccess-monitor
url = https://[$${slap-parameters:ipv6-random}]:$${:port}
index-filename = index.cgi
index-path = $${monitor-directory:www}/$${:index-filename}
db-path = $${monitor-directory:etc}/monitor.db
monitor-password-path = $${monitor-directory:etc}/.monitor.shadow
log-rss-directory = $${monitor-directory:log-rss-directory}

[monitor-directory]
recipe = slapos.cookbook:mkdirectory
# Standard directory needed by monitoring stack
home = $${buildout:directory}
etc = $${:home}/etc
bin = $${:home}/bin
srv = $${:home}/srv
var = $${:home}/var
log = $${:var}/log
run = $${:var}/run
service = $${:etc}/service/
etc-run = $${:etc}/run/
tmp = $${:home}/tmp
promise = $${:etc}/promise

cron-entries = $${:etc}/cron.d
crontabs = $${:etc}/crontabs
cronstamps = $${:etc}/cronstamps

ca-dir = $${:srv}/ssl
www = $${:var}/www

cgi-bin = $${:var}/cgi-bin
monitoring-cgi = $${:cgi-bin}/monitoring
knowledge0-cgi = $${:cgi-bin}/zero-knowledge
public-cgi = $${:cgi-bin}/monitor-public

monitor-custom-scripts = $${:etc}/monitor
monitor-result = $${:var}/monitor

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
symlink = $${monitor-directory:public-cgi} = $${monitor-directory:www}/monitor-public
autocreate = true

[cron]
recipe = slapos.cookbook:cron
dcrond-binary = ${dcron:location}/sbin/crond
cron-entries = $${monitor-directory:cron-entries}
crontabs = $${monitor-directory:crontabs}
cronstamps = $${monitor-directory:cronstamps}
catcher = $${cron-simplelogger:wrapper}
binary = $${monitor-directory:service}/crond

# Add log to cron
[cron-simplelogger]
recipe = slapos.cookbook:simplelogger
wrapper = $${monitor-directory:bin}/cron_simplelogger
log = $${monitor-directory:log}/cron.log

[cron-entry-monitor]
<= cron
recipe = slapos.cookbook:cron.d
name = launch-monitor
frequency = */5 * * * *
command = $${deploy-monitor-script:rendered} -a

[cron-entry-rss]
<= cron
recipe = slapos.cookbook:cron.d
name = build-rss
frequency = */5 * * * *
command = $${make-rss:rendered}

[setup-static-files]
recipe = plone.recipe.command
command = ln -s ${download-monitor-jquery:destination} $${monitor-directory:www}/static
update-command = $${:command}

[deploy-index]
recipe = slapos.recipe.template:jinja2
template = ${index:location}/${index:filename}
rendered = $${monitor-parameters:index-path}
update-apache-access = ${apache:location}/bin/htpasswd -cb $${monitor-parameters:htaccess-file} admin
# Force deploy information.html
mode = $${partition-parameters:mode}
context =
  key cgi_directory monitor-directory:cgi-bin
  raw index_template $${deploy-index-template:location}/$${deploy-index-template:filename}
  key monitor_password_path monitor-parameters:monitor-password-path
  key monitor_password_script_path deploy-monitor-password-cgi:rendered
  key apache_update_command :update-apache-access
  raw extra_eggs_interpreter ${buildout:directory}/bin/${extra-eggs:interpreter}
  raw default_page /static/welcome.html
  section rewrite_element monitor-rewrite-rule

[deploy-index-template]
recipe = hexagonit.recipe.download
url = ${index-template:location}/$${:filename}
destination = $${monitor-directory:www}
filename = ${index-template:filename}
download-only = true
mode = 0644

[deploy-status-cgi]
recipe = slapos.recipe.template:jinja2
template = ${status-cgi:location}/${status-cgi:filename}
rendered = $${monitor-directory:monitoring-cgi}/$${:filename}
filename = status.cgi
mode = 0744
context =
  key json_file monitor-parameters:json-path
  key monitor_bin monitor-parameters:executable
  key pwd monitor-directory:monitoring-cgi
  key this_file :filename
  raw python_executable ${buildout:executable}

[deploy-status-history-cgi]
recipe = slapos.recipe.template:jinja2
template = ${status-history-cgi:location}/${status-history-cgi:filename}
rendered = $${monitor-directory:monitoring-cgi}/$${:filename}
filename = status-history.cgi
mode = 0744
context =
  key monitor_db_path monitor-parameters:db-path
  key status_history_length zero-parameters:status-history-length
  raw python_executable ${buildout:executable}

[deploy-settings-cgi]
recipe = slapos.recipe.template:jinja2
template = ${settings-cgi:location}/${settings-cgi:filename}
rendered = $${monitor-directory:knowledge0-cgi}/$${:filename}
filename = settings.cgi
mode = 0744
context =
  raw config_cfg $${buildout:directory}/knowledge0.cfg
  raw timestamp $${buildout:directory}/.timestamp
  raw python_executable ${buildout:executable}
  key pwd monitor-directory:knowledge0-cgi
  key this_file :filename

[deploy-monitor-password-cgi]
recipe = slapos.recipe.template:jinja2
template = ${monitor-password-cgi:location}/${monitor-password-cgi:filename}
rendered = $${monitor-directory:knowledge0-cgi}/$${:filename}
filename = monitor-password.cgi
mode = 0744
context =
  raw python_executable ${buildout:executable}
  key pwd monitor-directory:knowledge0-cgi
  key this_file :filename

[deploy-monitor-script]
recipe = slapos.recipe.template:jinja2
template = ${monitor-bin:location}/${monitor-bin:filename}
rendered = $${monitor-parameters:executable}
mode = 0744
context =
  section directory monitor-directory
  section monitor_parameter monitor-parameters
  key monitoring_file_json monitor-parameters:json-path
  raw python_executable ${buildout:executable}

[deploy-logfile-cgi]
recipe = slapos.recipe.template:jinja2
template = ${logfile-cgi:location}/${logfile-cgi:filename}
rendered = $${monitor-directory:monitoring-cgi}/$${:filename}
filename = Files.cgi
script-path= monitoring/Files.cgi
mode = 0744
context =
  key monitor_bin monitor-parameters:executable
  key script_path :script-path
  section base_folder_list log-folder-cgi
  raw python_executable ${buildout:directory}/bin/${extra-eggs:interpreter}

[log-folder-cgi]
log-folder = $${monitor-directory:log}

[deploy-resource-monitoring-cgi]
recipe = slapos.recipe.template:jinja2
template = ${resources-cgi:location}/${resources-cgi:filename}
rendered = $${monitor-directory:monitoring-cgi}/$${:filename}
filename = resources.cgi
mode = $${deploy-settings-cgi:mode}
context =
  key monitor_bin monitor-parameters:executable
  raw python_executable ${buildout:directory}/bin/${extra-eggs:interpreter}
  key root_folder buildout:directory
  raw config_cfg $${buildout:directory}/$${public:filename}

[make-rss]
recipe = slapos.recipe.template:jinja2
template = ${make-rss-script:output}
rendered = $${monitor-directory:bin}/make-rss.sh
mode = 0744
context =
  section directory monitor-directory
  section monitor_parameters monitor-parameters

[monitor-directory-access]
recipe = plone.recipe.command
command = ln -s $${:source} $${monitor-directory:private-directory}
source =

# ErrorLog URL As RSS
[error-log-rss-base]
recipe = slapos.recipe.template:jinja2
template = ${errorlog-2rss:location}/${errorlog-2rss:filename}
rendered = $${monitor-directory:bin}/$${:script-name}
mode = 0700
extensions = jinja2.ext.do
base-link = $${monitor-parameters:url}/private/
extra-context =
context = 
  raw python_executable ${buildout:directory}/bin/${extra-eggs:interpreter}
  raw log_tool_folder ${log-tools:location}
  raw db_location $${monitor-directory:etc}/$${:script-name}.db
  key log_as_rss_ouptut monitor-directory:log-rss-directory
  key base_link :base-link
  $${:extra-context}

[log-as-rss-symlink]
< = monitor-directory-access
source = $${monitor-directory:log-rss-directory}

[error-log-rss]
<= error-log-rss-base
script-name = fileLog_AsRSS
# rotate_time: Max time (in hour) before remove an rss entry from database
# item_limit: Max number of rss entries to generate per cycle
extra-context = 
  section log_file_list error-log-list
  raw item_limit 10
  raw hour_interval 1
  raw rotate_time 24
  raw type zope

[error-log-list]
# XXX - error-log-name = FILE_PATH

[cron-entry-logAsRss]
<= cron
recipe = slapos.cookbook:cron.d
name = $${error-log-rss:script-name}
frequency = 30 * * * *
command = $${error-log-rss:rendered}

# Apache Dex
[apachedex-entries-base]
recipe = slapos.recipe.template:jinja2
template = ${run-apachedex:location}/${run-apachedex:filename}
rendered = $${monitor-directory:bin}/$${:script-name}
mode = 0700
extensions = jinja2.ext.do
extra-context =
context = 
  raw python_executable ${buildout:executable}
  raw apachedex_executable ${buildout:directory}/bin/apachedex
  key output_folder monitor-directory:apachedex-result
  $${:extra-context}
  
[apachedex-entries]
<= apachedex-entries-base
script-name = apachedex
extra-context =
  section parameter_dict apachedex-parameters
  key name :script-name
  
[apachedex-parameters]
default_parameter = 
# XXX - Sample log file with curent date: apache_access.log-%(date)s.gz
# which will be equivalent to apache_access.log-20150112.gz if the date is 2015-01-12
apache-log-list = 
base-list = 
skip-base-list = 
erp5-base-list = 

[cron-entry-apachedex]
<= cron
recipe = slapos.cookbook:cron.d
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
update-command = if [ -d $${:source} ]; then ln -s $${:source} $${monitor-directory:private-directory}/instance-logs; fi
source = $${monitor-directory:home}/.slapgrid/log/
location = $${:source}

[cadirectory]
recipe = slapos.cookbook:mkdirectory
requests = $${monitor-directory:ca-dir}/requests/
private = $${monitor-directory:ca-dir}/private/
certs = $${monitor-directory:ca-dir}/certs/
newcerts = $${monitor-directory:ca-dir}/newcerts/
crl = $${monitor-directory:ca-dir}/crl/

[certificate-authority]
recipe = slapos.cookbook:certificate_authority
openssl-binary = ${openssl:location}/bin/openssl
ca-dir = $${monitor-directory:ca-dir}
requests-directory = $${cadirectory:requests}
wrapper = $${monitor-directory:service}/certificate_authority
ca-private = $${cadirectory:private}
ca-certs = $${cadirectory:certs}
ca-newcerts = $${cadirectory:newcerts}
ca-crl = $${cadirectory:crl}

[ca-httpd]
<= certificate-authority
recipe = slapos.cookbook:certificate_authority.request
key-file = $${cadirectory:certs}/httpd.key
cert-file = $${cadirectory:certs}/httpd.crt
executable = $${monitor-directory:bin}/cgi-httpd
wrapper = $${monitor-directory:service}/cgi-httpd
# Put domain name
name = example.com

###########
# Deploy a webserver running cgi scripts for monitoring
###########
[public]
recipe = slapos.cookbook:zero-knowledge.write
filename = knowledge0.cfg
status-history-length = 5
collect-db-path = /srv/slapgrid/var/data-log/collector.db

[zero-parameters]
recipe = slapos.cookbook:zero-knowledge.read
filename = $${public:filename}

[monitor-rewrite-rule]

[partition-parameters]
recipe = slapos.recipe.template:jinja2
template = ${info-template:location}/${info-template:filename}
rendered = $${monitor-directory:monitoring-cgi}/information.html
mode = 0744
instance-id = 
context =
  raw extra_eggs_interpreter ${buildout:directory}/bin/${extra-eggs:interpreter}
  key computer_id slap-connection:computer-id
  key partition_id slap-connection:partition-id
  key home buildout:directory
  key master_url slap-connection:server-url
  key software_url slap-connection:software-release-url
  key ipv4 slap-parameters:ipv4-random
  key ipv6 slap-parameters:ipv6-random
  key slapparameter_dict slap-parameters:configuration
  key instance_reference :instance-id
  section extra_parameters extra-parameters-output

[extra-parameters-output]

# XXX could it be something lighter?
[monitor-httpd-configuration]
pid-file = $${monitor-directory:run}/cgi-httpd.pid
cgid-pid-file = $${monitor-directory:run}/cgi-httpd-cgid.pid
error-log = $${monitor-directory:log}/cgi-httpd-error-log
listening-ip = $${slap-parameters:ipv6-random}
certificate = $${ca-httpd:cert-file}
key = $${ca-httpd:key-file}

[httpd-environment]
PYTHONPATH = ${log-tools:location}:${collect-tools:location}

[monitor-httpd-configuration-file]
recipe = slapos.recipe.template:jinja2
template = ${monitor-httpd-template:destination}/${monitor-httpd-template:filename}
rendered = $${monitor-directory:etc}/cgi-httpd.conf
mode = 0744
context =
  section directory monitor-directory
  section monitor_parameters monitor-parameters
  section httpd_configuration monitor-httpd-configuration
  section monitor_rewrite_rule monitor-rewrite-rule
  section environment_dict httpd-environment

[cgi-httpd-wrapper]
recipe = slapos.cookbook:wrapper
apache-executable = ${apache:location}/bin/httpd
command-line = $${:apache-executable} -f $${monitor-httpd-configuration-file:rendered} -DFOREGROUND
wrapper-path = $${ca-httpd:executable}

[cgi-httpd-graceful-wrapper]
recipe = slapos.recipe.template:jinja2
template = ${template-wrapper:output}
rendered = $${monitor-directory:etc-run}/cgi-httpd-graceful
mode = 0700
context =
    key content :command
command = kill -USR1 $(cat $${monitor-httpd-configuration:pid-file})


[monitor-promise]
recipe = slapos.cookbook:check_url_available
path = $${monitor-directory:promise}/monitor
url = $${monitor-parameters:url}/$${monitor-parameters:index-filename}
check-secure = 1
dash_path = ${dash:location}/bin/dash
curl_path = ${curl:location}/bin/curl

[publish-connection-informations]
recipe = slapos.cookbook:publish
monitor_url = $${monitor-parameters:url}