From 8b2da9f7bd928cbf2cfbb9d32698987472c22e94 Mon Sep 17 00:00:00 2001 From: feix Date: Mon, 21 Aug 2017 23:19:27 +0800 Subject: [PATCH] 1. Using sqlite db for servers management 2. Manipulating server info on web page --- .gitignore | 4 ++- Readme.md | 33 ++++++++++-------- ...{supervisor.php.example => supervisor.php} | 32 ++++++++--------- application/controllers/control.php | 23 +++++++++++++ application/views/welcome.php | 34 +++++++++++++++++-- sqlite.sql | 6 ++++ 6 files changed, 96 insertions(+), 36 deletions(-) rename application/config/{supervisor.php.example => supervisor.php} (52%) create mode 100644 sqlite.sql diff --git a/.gitignore b/.gitignore index 53b8d9e..1734d08 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ -application/config/supervisor.php composer.phar composer.lock /vendor +.idea +supervisord-monitor.iml +*.db diff --git a/Readme.md b/Readme.md index 58b5b50..06a09a2 100644 --- a/Readme.md +++ b/Readme.md @@ -14,35 +14,38 @@ ## Install -1.Clone supervisord-monitor to your vhost/webroot: -``` +1. Clone supervisord-monitor to your vhost/webroot: + +```bash git clone https://github.com/mlazarov/supervisord-monitor.git ``` -2.Copy application/config/supervisor.php.example to application/config/supervisor.php -``` -cp supervisord-monitor/application/config/supervisor.php.example supervisord-monitor/application/config/supervisor.php +2. Construct sqlite DB + +```bash +cd supervisord-monitor +sqlite3 sqlite.db < sqlite.sql ``` -3.Enable/Uncomment inet_http_server (found in supervisord.conf) for all your supervisord servers. +3. Configure your web server to point one of your vhosts to 'public_html' directory. + +4. Open web browser and enter your vhost url. + +5. Enable/Uncomment inet_http_server (found in supervisord.conf) for all your supervisord servers. + + Do not forget to restart supervisord service after changing supervisord.conf + ```ini [inet_http_server] port=*:9001 username="yourusername" password="yourpass" ``` -Do not forget to restart supervisord service after changing supervisord.conf - -4.Edit supervisord-monitor configuration file and add all your supervisord servers -``` -vim application/config/supervisor.php -``` - -5.Configure your web server to point one of your vhosts to 'public_html' directory. -6.Open web browser and enter your vhost url. +6. Add your supervisord servers by using "Add" button. ## Redmine integration + 1.Open configuration file: ``` vim application/config/supervisor.php diff --git a/application/config/supervisor.php.example b/application/config/supervisor.php similarity index 52% rename from application/config/supervisor.php.example rename to application/config/supervisor.php index f2b3a12..f8e530f 100644 --- a/application/config/supervisor.php.example +++ b/application/config/supervisor.php @@ -13,22 +13,20 @@ // Show hostname after server name $config['show_host'] = false; -$config['supervisor_servers'] = array( - 'server01' => array( - 'url' => 'http://server01.app/RPC2', - 'port' => '9001', - 'username' => 'yourusername', - 'password' => 'yourpass' - ), - 'server02' => array( - 'url' => 'http://server02.app/RPC2', - 'port' => '9001' - ), - 'server03' => array( - 'url' => 'http://server03.app/RPC2', - 'port' => '9001' - ), -); +$config['sqlite_db_path'] = '../sqlite.db'; +$config['sqlite_db_table'] = 'servers'; + +$servers = array(); +$sqlite_db = new PDO('sqlite:' . $config['sqlite_db_path']); +foreach ($sqlite_db->query('select * from ' . $config['sqlite_db_table']) as $server) { + $servers[$server['name']] = array( + 'url' => 'http://' . $server['ip'] . '/RPC2', + 'port' => $server['port'], + 'username' => $server['username'], + 'password' => $server['password'] + ); +} +$config['supervisor_servers'] = $servers; // Set timeout connecting to remote supervisord RPC2 interface $config['timeout'] = 3; @@ -38,5 +36,3 @@ // Default Redmine assigne ID $config['redmine_assigne_id'] = '69'; - - diff --git a/application/controllers/control.php b/application/controllers/control.php index e0f7985..d1bc0d3 100644 --- a/application/controllers/control.php +++ b/application/controllers/control.php @@ -32,4 +32,27 @@ function Clear($server,$worker){ $this->_request($server,'clearProcessLogs',array($worker)); Redirect('/'); } + function AddServer() { + try { + $name = $_POST['name']; + $ip = $_POST['ip']; + $port = $_POST['port']; + $username = $_POST['username']; + $password = $_POST['password']; + $sqlite_db = new PDO('sqlite:' . $this->config->item('sqlite_db_path')); + $sqlite_db->exec("insert into " . $this->config->item('sqlite_db_table') . " values ('$name', '$ip', '$port', '$username', '$password')"); + Redirect('/'); + } catch (Exception $e) { + echo json_encode("add server error"); + } + } + function DelServer($server) { + try { + $sqlite_db = new PDO('sqlite:' . $this->config->item('sqlite_db_path')); + $sqlite_db->exec("delete from " . $this->config->item('sqlite_db_table') . " where name='$server'"); + Redirect('/'); + } catch (Exception $e) { + echo json_encode("delete server error"); + } + } } diff --git a/application/views/welcome.php b/application/views/welcome.php index 686e8f6..49bef24 100644 --- a/application/views/welcome.php +++ b/application/views/welcome.php @@ -38,6 +38,7 @@ ;?>
  • Refresh (config->item('refresh');?>)  
  • Contact
  • +
  • Add
  • @@ -46,6 +47,19 @@
    + + config->item('show_host')){ ?> + ';} echo ' '.$version[$name].''; @@ -212,16 +227,31 @@ function startTimer(){ function timer(){ $refresh--; $('#refresh').html('('+$refresh+')'); - if($refresh<=0){ + if($refresh<=0 && !$('#add-server-form').is(':visible')){ stopTimer(); location.href=""; } - } function nl2br (str, is_xhtml) { var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '
    ' : '
    '; return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2'); } + $(function(){ + $('#add-server').on('click', function(e) { + $('#add-server-form').toggle(); + }); + $('#add-server-form').on('submit', function(e){ + e.preventDefault(); + $.ajax({ + type: 'POST', + url: '/control/addserver', + data: $('#add-server-form').serialize(), + success: function(data){ + location.href=""; + } + }); + }); + }); diff --git a/sqlite.sql b/sqlite.sql new file mode 100644 index 0000000..bc0a4f9 --- /dev/null +++ b/sqlite.sql @@ -0,0 +1,6 @@ +create table servers (name TEXT PRIMARY KEY NOT NULL, ip TEXT NOT NULL, port TEXT NOT NULL, username TEXT NOT NULL, password TEXT NOT NULL); +/* +insert into servers (name, ip, port, username, password) values ('server01', 'server01.app', 9001, 'yourusername', 'yourpass'); +insert into servers (name, ip, port, username, password) values ('server02', 'server02.app', 9001, '', ''); +insert into servers (name, ip, port, username, password) values ('server03', 'server03.app', 9001, '', ''); +*/