diff options
author | Alexander Sulfrian <alex@spline.inf.fu-berlin.de> | 2013-08-30 00:54:37 +0000 |
---|---|---|
committer | Alexander Sulfrian <alex@spline.inf.fu-berlin.de> | 2013-08-30 00:54:51 +0000 |
commit | 1c70b0447570619f537147db8ca491c054467a12 (patch) | |
tree | 3fecb0d65303531487ff26592181e93fca911acb | |
download | dokuwiki-1c70b0447570619f537147db8ca491c054467a12.tar.gz dokuwiki-1c70b0447570619f537147db8ca491c054467a12.tar.bz2 dokuwiki-1c70b0447570619f537147db8ca491c054467a12.zip |
initial commit
-rw-r--r-- | templates/host.tpl.php | 85 | ||||
-rw-r--r-- | templates/start.tpl.php | 67 | ||||
-rwxr-xr-x | update.php | 355 |
3 files changed, 507 insertions, 0 deletions
diff --git a/templates/host.tpl.php b/templates/host.tpl.php new file mode 100644 index 0000000..68af670 --- /dev/null +++ b/templates/host.tpl.php @@ -0,0 +1,85 @@ +====== <?php echo str_replace('.spline.inf.fu-berlin.de', '', $hostname) ?> ====== + +<?php echo $data['description'] ?> + +===== Summary ===== + + * **hostname:** <?php echo $hostname . "\n" ?> + * **os:** <?php echo_with_default($os, 'unknown'); echo "\n" ?> + * **arch:** <?php echo_with_default($arch, 'unknown'); echo "\n" ?> +<?php if (isset($vserver)) { ?> + * **vserver:** <?php echo $vserver . "\n" ?> +<?php } ?> +<?php if (isset($vserver_host)) { ?> + * **vserver host:** <?php echo $vserver_host . "\n"?> +<?php } ?> +<?php if (is_array($maintainers)) { ?> + * **maintainers:** <?php + + $first = true; + foreach ($maintainers as $maintainer) { + list($name, $mail) = get_contact_info($maintainer); + if (!$first) { + echo ', '; + } + $first = false; + + echo "[[$mail|$name]]"; + } + echo "\n"; +?> +<?php } ?> +<?php if (isset($groups)) { ?> + * **bcfg2-groups:** [[https://bcfg2.spline.de/client/<?php echo $hostname ?>|view client in bcfg2]] +<?php foreach ($groups as $group) { ?> + * <?php echo $group . "\n" ?> +<?php } ?> +<?php } ?> + +===== network interfaces ===== + +<?php $filtered_addresses = select_by($addresses, 'vserver', false); ?> +<?php if (count($filtered_addresses) > 0) { ?> +<?php foreach (group_by($filtered_addresses, 'interface') as $group => $value) { ?> + * **<?php echo $group ?>** +<?php foreach ($value as $address) { ?> + * <?php echo $address['address'] . '/' . $address['netmask'] . "\n" ?> +<?php } ?> +<?php } ?> +<?php } else { ?> +No network interfaces configured. +<?php } ?> + +===== open ports ===== + +<?php if (is_array($ports)) { ?> +<sortable 1> +^ Port ^ IP ^ Process ^ Protocol ^ +<?php foreach ($ports as $port) { ?> +| <?php echo $port['port'] ?> | <?php echo $port['ip'] ?> | <?php echo $port['process'] ?> | <?php echo $port['proto'] ?> | +<?php } ?> +</sortable> +<?php } else { ?> +No open ports +<?php } ?> + +===== services ===== + +<?php if (is_array($services) && count($services) > 0) { ?> +<sortable> +^ Name ^ Port ^ Visibility ^ +<?php foreach ($services as $category => $service_category) { ?> +<?php if (is_array($service_category)) { ?> +<?php foreach ($service_category as $service) { ?> +<?php if (preg_match('/(.*): (.*)/', $service, $match)) { ?> +| <?php echo $match[1] ?> | <?php echo $match[2] ?> | <?php echo $category ?> | +<?php } else { ?> +| <?php echo $service ?> | default | <?php echo $category ?> | +<?php } ?> +<?php } ?> +<?php } ?> +<?php } ?> +</sortable> +<?php } else { ?> +no services provoided +<?php } ?> diff --git a/templates/start.tpl.php b/templates/start.tpl.php new file mode 100644 index 0000000..f3a8df4 --- /dev/null +++ b/templates/start.tpl.php @@ -0,0 +1,67 @@ +====== Hostinfo ====== + +<WRAP centeralign>{confsearch> @hostinfo > Search in hostinfo}</WRAP> + +===== Übersicht ===== + +<flattable key="Hostname" os="OS" arch="Arch" vserver="VServer"="--" maint="Maintainers"=0 nagios="Monitoring" bcfg2="Bcfg2" doc="Doku"="FIXME"> +<?php foreach ($HOSTINFO as $host => $data) { ?> +[[<?php echo $host ?>|<?php echo str_replace('.spline.inf.fu-berlin.de', '', $host) ?>]]: + @os: <?php echo $data['os']; echo "\n" ?> + @arch: <?php echo $data['arch']; echo "\n" ?> +<?php if (isset($data['vserver']) && !empty($data['vserver'])) { ?> + @vserver: <?php echo $data['vserver']; echo "\n" ?> +<?php } ?> + @maint: <?php echo count($data['maintainers']); echo "\n" ?> + @nagios: [[https://monitoring.spline.inf.fu-berlin.de/icinga/<?php echo str_replace('.spline.inf.fu-berlin.de', '', $host) ?>|Link]] + @bcfg2: [[https://bcfg2.spline.inf.fu-berlin.de/client/<?php echo $host ?>|Link]] +<?php if (isset($data['doc']) && strlen(trim($data['doc'])) > 100) { ?> + @doc: :-D +<?php } ?> + +<?php } ?> +</flattable> + +===== IPs ===== + +<flattable key="IP" host="Hostname" sort="1"> +<?php foreach ($HOSTINFO as $host => $data) { ?> +<?php if (isset($data['addresses']) && is_array($data['addresses'])) { ?> +<?php foreach ($data['addresses'] as $address) { ?> +<?php if (!isset($address['vserver'])) { ?> +<?php echo $address['address'] ?>: + @host: [[<?php echo $host ?>|<?php echo str_replace('.spline.inf.fu-berlin.de', '', $host) ?>]] + +<?php } ?> +<?php } ?> +<?php } ?> +<?php } ?> +</flattable> + +===== Bcfg2 Groups ===== + +<?php foreach (get_bcfg2_groups($HOSTINFO) as $group => $hosts) { ?> +==== <?php echo $group ?> ==== + +<?php if ($hosts == 'all') { ?> +[[start#uebersicht|alle Server]] +<?php } else { ?> +<flattable key="Hostname" os="OS" arch="Arch" vserver="VServer"="--" maint="Maintainers"=0 nagios="Monitoring" bcfg2="Bcfg2" doc="Doku"="FIXME"> +<?php foreach ($hosts as $host => $data) { ?> +[[<?php echo $host ?>|<?php echo str_replace('.spline.inf.fu-berlin.de', '', $host) ?>]]: + @os: <?php echo $data['os']; echo "\n" ?> + @arch: <?php echo $data['arch']; echo "\n" ?> +<?php if (isset($data['vserver']) && !empty($data['vserver'])) { ?> + @vserver: <?php echo $data['vserver']; echo "\n" ?> +<?php } ?> + @maint: <?php echo count($data['maintainers']); echo "\n" ?> + @nagios: [[https://monitoring.spline.inf.fu-berlin.de/icinga/<?php echo str_replace('.spline.inf.fu-berlin.de', '', $host) ?>|Link]] + @bcfg2: [[https://bcfg2.spline.inf.fu-berlin.de/client/<?php echo $host ?>|Link]] +<?php if (isset($data['doc']) && strlen(trim($data['doc'])) > 100) { ?> + @doc: :-D +<?php } ?> + +<?php } ?> +</flattable> +<?php } ?> +<?php } ?> diff --git a/update.php b/update.php new file mode 100755 index 0000000..15fa8d8 --- /dev/null +++ b/update.php @@ -0,0 +1,355 @@ +#!/usr/bin/env php +<?php +if ('cli' != php_sapi_name()) die(); + +// fake enviroment +$_SERVER['HTTP_HOST'] = 'doku.spline.inf.fu-berlin.de'; +$_SERVER['REMOTE_ADDR'] = '127.0.0.1'; + +ini_set('memory_limit','128M'); +if(!defined('DOKU_INC')) define('DOKU_INC', '/usr/share/dokuwiki/'); +require_once(DOKU_INC.'inc/init.php'); +require_once(DOKU_INC.'inc/cliopts.php'); +require_once('SymfonyComponents/YAML/sfYaml.php'); +session_write_close(); + +#------------------------------------------------------------------------------ +# handle options + +$short_opts = 'hcq'; +$long_opts = array('help', 'clear', 'quiet'); +$OPTS = Doku_Cli_Opts::getOptions(__FILE__, $short_opts, $long_opts); +if ( $OPTS->isError() ) { + fwrite( STDERR, $OPTS->getMessage() . "\n"); + _usage(); + exit(1); +} +$CLEAR = false; +$QUIET = false; +$PAGES = array(); + +foreach ($OPTS->options as $key => $val) { + switch ($key) { + case 'h': + case 'help': + _usage(); + exit; + case 'c': + case 'clear': + $CLEAR = true; + break; + case 'q': + case 'quiet': + $QUIET = true; + break; + } +} + +if (!$OPTS->hasArgs()) { + _usage(); + exit; +} + +#------------------------------------------------------------------------------ +# Doku_Indexer_Mass_Remover + +class Doku_Indexer_Mass_Remover extends Doku_Indexer { + function deletePages($pages) { + if (!$this->lock()) { + die('Locked'); + } + + foreach($pages as $page) { + _quietecho('Removing from index: ' . $page); + $this->deletePageNoLock($page); + _quietecho(" done.\n"); + } + + $this->unlock(); + } +} + +#------------------------------------------------------------------------------ +# main() + +$path_to_hostinfo = $OPTS->arg(0); +_load_hostinfo_data($path_to_hostinfo); + +_find_current_pages(); +if (!$CLEAR) { + _update_content(); + _render('start', 'start', array('HOSTINFO' => $HOSTINFO)); + _update_index(); +} +_remove_outdated(); + + +#------------------------------------------------------------------------------ +# _find_current_pages + +function _find_current_pages() { + global $conf, $PAGES; + + $data = array(); + search($data, $conf['datadir'], 'search_allpages', array('skipacl' => true), 'hostinfo'); + + foreach ($data as $page) { + $PAGES[$page['id']] = 'delete'; + } +} + +#------------------------------------------------------------------------------ +# _load_hostinfo_data + +function _load_hostinfo_data($path) { + global $HOSTINFO; + + $hosts_file = $path . '/metadata/hosts'; + if (!file_exists($hosts_file)) { + die("Invalid hostinfo path: $path\n"); + } + + $hosts = sfYaml::load($hosts_file); + foreach ($hosts['hosts'] as $host) { + $HOSTINFO[$host] = sfYaml::load($path . '/' . $host); + } +} + +#------------------------------------------------------------------------------ +# _update_content + +function _update_content() { + global $conf, $HOSTINFO; + + // render sites + foreach ($HOSTINFO as $host => $host_data) { + _render($host, 'host', $host_data); + } +} + +#------------------------------------------------------------------------------ +# _render + +function _render($target, $file, $vars=null) { + global $conf, $PAGES; + + _quietecho("Rendering $file into $target"); + $content = _render_template($file, $vars); + if ($content === false) { + _quietecho(" FAILED (missing template)\n"); + return false; + } + else { + $pagename = cleanID('hostinfo:' . $target); + $page = wikiFN($pagename); + + $old_content = ''; + if (file_exists($page)) { + $old_content = file_get_contents($page); + } + + if ($content != $old_content) { + $PAGES[$pagename] = 'update'; + saveWikiText($pagename, $content, 'auto generated'); + _quietecho(" done\n"); + } + else { + unset($PAGES[$pagename]); + _quietecho(" no change\n"); + } + return true; + } +} + +#------------------------------------------------------------------------------ +# default + +function echo_with_default($value, $default) { + if (!isset($value) || empty($value)) { + echo $default; + } + else { + echo $value; + } +} + +#------------------------------------------------------------------------------ +# get_contact_info + +function get_contact_info($maintainer) { + if (preg_match('/^([^:]*): (.*@.*)$/', $maintainer, $match)) { + return array($match[1], $match[2]); + } + + return array($maintainer, $maintainer . '@spline.inf.fu-berlin.de'); +} + +#------------------------------------------------------------------------------ +# get_bcfg2_groups + +function get_bcfg2_groups($hostinfo) { + $groups = array(); + + foreach ($hostinfo as $host => $data) { + foreach ($data['groups'] as $group) { + $groups[$group] = array(); + } + } + + foreach (array_keys($groups) as $group) { + foreach ($hostinfo as $host => $data) { + if (in_array($group, $data['groups'])) { + $groups[$group][$host] = $data; + } + } + } + + foreach ($groups as $group => $hosts) { + if (count($hosts) < 2) { + unset($groups[$group]); + } + + if (count($hosts) == count($hostinfo)) { + $groups[$group] = 'all'; + } + } + + return $groups; +} + +#------------------------------------------------------------------------------ +# group_by + +function group_by($data, $key) { + $result = array(); + + foreach ($data as $value) { + if (array_key_exists($key, $value)) { + $result[$value[$key]][] = $value; + } + } + + return $result; +} + +#------------------------------------------------------------------------------ +# select_by + +function select_by($data, $key, $exists = true) { + $result = array(); + + if (is_array($data)) { + foreach ($data as $value) { + if (array_key_exists($key, $value) === $exists) { + $result[] = $value; + } + } + } + + return $result; +} + +#------------------------------------------------------------------------------ +# _render_template + +function _render_template($file, $vars=null) { + // validate $file + $file = preg_replace('/^(?:.+\/)?([a-zA-Z_-]*)(?:\.[a-zA-Z])?$/', '$1', $file); + $file = 'templates/' . $file . '.tpl.php'; + + if (!file_exists($file)) { + // template does not exists + return false; + } + + if (is_array($vars) && !empty($vars)) { + if (array_key_exists('file', $vars)) { + // if $vars would contain a 'file' key, this would overwrite the + // first parameter of the template file + unset($vars['file']); + } + + extract($vars); + } + + ob_start(); + include $file; + return ob_get_clean(); +} + +#------------------------------------------------------------------------------ +# _update_index + +function _update_index() { + global $conf, $PAGES; + + + foreach ($PAGES as $page => $state) { + if ($state != 'delete') { + _quietecho('Add to index: ' . $page); + idx_addPage($page, false, true); + _quietecho(" done.\n"); + } + } +} + +#------------------------------------------------------------------------------ +# _remove_index + +function _remove_index() { + global $PAGES; + + $data = array(); + foreach ($PAGES as $page => $state) { + if ($state == 'delete') { + $data[] = $page; + } + } + + $idx = new Doku_Indexer_Mass_Remover(); + $idx->deletePages($data); +} + +#------------------------------------------------------------------------------ +# _remove_outdated + +function _remove_outdated() { + global $PAGES; + + foreach ($PAGES as $page => $state) { + if ($state == 'delete') { + _quietecho('Removing old page: ' . $page); + unlink(wikiFN($page)); + _quietecho(" done.\n"); + } + } + _remove_index(); +} + +#------------------------------------------------------------------------------ +# _usage + +function _usage() { + print "Usage: update.php <options> <path to hostinfo> + +Updates the hostinfo information by first removing all pages in the hostinfo +namespace from the fulltext index, generating the new files and then +reindexing all pages. +When the -c option is given the index is only cleared and nothing is updated. + +OPTIONS + -h, --help show this help and exit + -c, --clear only clear the index + -q, --quiet don't produce any output +"; +} + +#------------------------------------------------------------------------------ +# _quietecho + +function _quietecho($msg) { + global $QUIET; + if(!$QUIET) echo $msg; +} + +?> |