Skip to content

Commit 39b6666

Browse files
committed
Merge pull request #550 from opinkerfi/nagios-daemon-interface
Nagios process interaction through adagios.daemon
2 parents 44e6765 + d37ba3b commit 39b6666

File tree

11 files changed

+96
-58
lines changed

11 files changed

+96
-58
lines changed

adagios/bi/__init__.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import simplejson as json
2222
import pynag.Model
2323
import pynag.Parsers
24-
import pynag.Control
24+
import adagios.daemon
2525
import adagios.pnp.functions
2626
import adagios.settings
2727
import time
@@ -760,11 +760,7 @@ def create_host(self):
760760
#host.hostgroups = 'domains,nameservers,mailservers,http-servers,https-servers'
761761
host.save()
762762

763-
daemon = pynag.Control.daemon(
764-
nagios_bin=adagios.settings.nagios_binary,
765-
nagios_cfg=adagios.settings.nagios_config,
766-
nagios_init=adagios.settings.nagios_init_script,
767-
)
763+
daemon = adagios.daemon.Daemon()
768764

769765
result = daemon.reload()
770766
time.sleep(1)

adagios/context_processors.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
from adagios import notifications, settings, add_plugin
2323
from adagios.misc.rest import add_notification, clear_notification
24+
from adagios.daemon import Daemon
2425

2526
import pynag.Model.EventHandlers
2627
import pynag.Parsers
@@ -292,11 +293,8 @@ def check_destination_directory(request):
292293
def check_nagios_running(request):
293294
""" Notify user if nagios is not running """
294295
try:
295-
if pynag.Model.config is None:
296-
pynag.Model.config = pynag.Parsers.config(
297-
adagios.settings.nagios_config)
298-
nagios_pid = pynag.Model.config._get_pid()
299-
return {"nagios_running": (nagios_pid is not None)}
296+
d = Daemon()
297+
return {"nagios_running": (d.running())}
300298
except Exception:
301299
return {}
302300

adagios/daemon.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Adagios is a web based Nagios configuration interface
2+
#
3+
# Copyright (C) 2014, Tomas Edwardsson <tommi@tommi.org>
4+
#
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU Affero General Public License as
7+
# published by the Free Software Foundation, either version 3 of the
8+
# License, or (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU Affero General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU Affero General Public License
16+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
18+
"""Methods for controlling and getting status of the Nagios daemon"""
19+
20+
from pynag.Control import daemon
21+
from adagios import settings
22+
23+
class Daemon(daemon):
24+
def __init__(self):
25+
super(Daemon, self).__init__()
26+
27+
# Mapping needed
28+
if settings.nagios_binary:
29+
self.nagios_bin = settings.nagios_binary
30+
if settings.nagios_config:
31+
self.nagios_cfg = settings.nagios_config
32+
if settings.nagios_init_script:
33+
self.nagios_init = settings.nagios_init_script
34+
if settings.nagios_service:
35+
self.service_name = settings.nagios_service
36+
37+
# vim: sts=4 expandtab autoindent

adagios/etc/adagios/adagios.conf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@ enable_loghandler = False
3434
# a nagios reload after every single change.
3535
auto_reload = False
3636

37+
# nagios_service - Service name used to start/stop nagios
38+
# Adagios uses this to reload nagios after configuration changes
39+
nagios_service = "nagios"
40+
3741
# nagios_init_script - Path to init script used to start/stop nagios
3842
# Adagios uses this to reload nagios after configuration changes
39-
nagios_init_script="sudo /etc/init.d/nagios"
43+
nagios_init_script=""
4044

4145
# nagios_binary - Path to your nagios binary
4246
# Adagios uses this to run "nagios -v /etc/nagios/nagios.cfg"

adagios/etc/sudoers.d/adagios

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
Defaults:nagios !requiretty
2-
nagios ALL = (root) NOPASSWD: /etc/init.d/nagios
1+
Defaults:%nagios !requiretty
2+
%nagios ALL = (root) NOPASSWD: /etc/init.d/nagios
3+
%nagios ALL = (root) NOPASSWD: /sbin/service nagios *
4+
%nagios ALL = (root) NOPASSWD: /usr/sbin/nagios -v /etc/nagios/nagios.cfg
35

adagios/misc/forms.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@
2828
import os.path
2929
from adagios import settings
3030
import adagios.utils
31-
from pynag import Model, Control
31+
from pynag import Model
3232
from django.core.mail import EmailMultiAlternatives
3333
import pynag.Parsers
3434
import pynag.Control.Command
35+
import adagios.daemon
3536

3637

3738
TOPIC_CHOICES = (
@@ -120,8 +121,12 @@ class AdagiosSettingsForm(forms.Form):
120121
required=False, initial=settings.destination_directory, help_text=_("Where to save new objects that adagios creates."))
121122
nagios_url = forms.CharField(required=False, initial=settings.nagios_url,
122123
help_text=_("URL (relative or absolute) to your nagios webcgi. Adagios will use this to make it simple to navigate from a configured host/service directly to the cgi."))
124+
nagios_service = forms.CharField(
125+
required=False,
126+
help_text=_("The name of the nagios service, commonly nagios or nagios3. Adagios will use this when stopping/starting/reloading nagios"))
123127
nagios_init_script = forms.CharField(
124-
help_text=_("Path to you nagios init script. Adagios will use this when stopping/starting/reloading nagios"))
128+
required=False,
129+
help_text=_("You should define either 'Nagios service' or 'Nagios init script'. Path to you nagios init script. Adagios will use this when stopping/starting/reloading nagios"))
125130
nagios_binary = forms.CharField(
126131
help_text=_("Path to you nagios daemon binary. Adagios will use this to verify config with 'nagios -v nagios_config'"))
127132
livestatus_path = forms.CharField(
@@ -204,7 +209,7 @@ def clean_nagios_init_script(self):
204209
filename = self.cleaned_data['nagios_init_script']
205210
if filename.startswith('sudo'):
206211
self.check_file_exists(filename.split()[1])
207-
else:
212+
elif filename:
208213
self.check_file_exists(filename)
209214
return filename
210215

@@ -499,31 +504,31 @@ class NagiosServiceForm(forms.Form):
499504

500505
def save(self):
501506
#nagios_bin = self.cleaned_data['nagios_bin']
507+
daemon = adagios.daemon.Daemon()
502508
if "reload" in self.data:
503-
command = "reload"
509+
command = daemon.reload
510+
self.command = "reload"
504511
elif "restart" in self.data:
505-
command = "restart"
512+
command = daemon.restart
513+
self.command = "restart"
506514
elif "stop" in self.data:
507-
command = "stop"
515+
command = daemon.stop
516+
self.command = "stop"
508517
elif "start" in self.data:
509-
command = "start"
518+
command = daemon.start
519+
self.command = "start"
510520
elif "status" in self.data:
511-
command = "status"
521+
command = daemon.status
522+
self.command = "status"
512523
elif "verify" in self.data:
513-
command = "verify"
524+
command = daemon.verify_config
525+
self.command = "verify"
514526
else:
515527
raise Exception(_("Unknown command"))
516-
self.command = command
517-
nagios_init = settings.nagios_init_script
518-
nagios_binary = settings.nagios_binary
519-
nagios_config = settings.nagios_config or pynag.Model.config.cfg_file
520-
if command == "verify":
521-
command = "%s -v '%s'" % (nagios_binary, nagios_config)
522-
else:
523-
command = "%s %s" % (nagios_init, command)
524-
code, stdout, stderr = pynag.Utils.runCommand(command)
525-
self.stdout = stdout or ""
526-
self.stderr = stderr or ""
528+
529+
code = command()
530+
self.stdout = daemon.stdout or ""
531+
self.stderr = daemon.stderr or ""
527532
self.exit_code = code
528533

529534
def verify(self):

adagios/misc/helpers.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
import re
2929
from pynag import Model
3030
from pynag import Parsers
31-
from pynag import Control
3231
from pynag import Utils
3332
from pynag import __version__
3433
from socket import gethostbyname_ex
3534
import adagios.settings
35+
from adagios.daemon import Daemon
3636
from django.utils.translation import ugettext as _
3737

3838

@@ -240,11 +240,7 @@ def set_maincfg_attribute(attribute, new_value, old_value='None', append=False):
240240

241241
def reload_nagios():
242242
""" Reloads nagios. Returns "Success" on Success """
243-
daemon = Control.daemon(
244-
nagios_cfg=Model.config.cfg_file,
245-
nagios_init=adagios.settings.nagios_init_script,
246-
nagios_bin=adagios.settings.nagios_binary
247-
)
243+
daemon = Daemon()
248244
result = {}
249245
if daemon.reload() == 0:
250246
result['status'] = _("success")

adagios/misc/templates/nagios_service.html

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,20 @@ <h4>{% trans "Errors" %}</h4>
4747
{% endif %}
4848
</div>
4949

50-
{% if not stdout and not stderr %}
50+
{% if stderr|slice:":5" == 'sudo:' %}
5151
<div class="alert alert-danger">
5252
<p>
53-
{% blocktrans %}Something does not smell right. We ran {{ command }} and got no output.
53+
{% blocktrans %}Something does not smell right. We ran {{ command }} and got the error above.
5454
Usually when this happens there is a problem with sudoers configuration.{% endblocktrans %}
5555
</p>
5656
<p>
5757
{% blocktrans %}Inspect webserver apache log and check if there are any sudo errors.
5858
Your <a>/etc/sudoers.d/adagios</a> should have something like this:{% endblocktrans %}
5959
</p>
6060
<pre>
61-
Defaults:{{ local_user }} !requiretty
62-
{{ local_user }} ALL = (root) NOPASSWD: /etc/init.d/nagios3
61+
Defaults:{{ local_user }} !requiretty
62+
{{ local_user }} ALL = (root) NOPASSWD: /sbin/service nagios *
63+
{{ local_user }} ALL = (root) NOPASSWD: {{ nagios_bin }} -v *
6364
</pre>
6465
</div>
6566
{% endif %}
@@ -84,4 +85,4 @@ <h5>{% trans "Diagnostics" %}</h5>
8485

8586
{% endblock %}
8687

87-
{% block footer %} {{ block.super }} {% endblock %}
88+
{% block footer %} {{ block.super }} {% endblock %}

adagios/misc/views.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
import pynag.Model
3333
import pynag.Utils
34-
import pynag.Control
34+
import adagios.daemon
3535
import pynag.Model.EventHandlers
3636
import pynag.Utils
3737
import os.path
@@ -182,9 +182,7 @@ def nagios_service(request):
182182
c = {}
183183
c['errors'] = []
184184
c['messages'] = []
185-
nagios_bin = adagios.settings.nagios_binary
186-
nagios_init = adagios.settings.nagios_init_script
187-
nagios_cfg = adagios.settings.nagios_config
185+
188186
if request.method == 'GET':
189187
form = forms.NagiosServiceForm(initial=request.GET)
190188
else:
@@ -194,20 +192,20 @@ def nagios_service(request):
194192
c['stdout'] = form.stdout
195193
c['stderr'] = form.stderr
196194
c['command'] = form.command
195+
c['exit_code'] = form.exit_code
197196

198197
for i in form.stdout.splitlines():
199198
if i.strip().startswith('Error:'):
200199
c['errors'].append(i)
201200
c['form'] = form
202-
service = pynag.Control.daemon(
203-
nagios_bin=nagios_bin, nagios_cfg=nagios_cfg, nagios_init=nagios_init)
204-
c['status'] = s = service.status()
205-
if s == 0:
201+
daemon = adagios.daemon.Daemon()
202+
c['nagios_bin'] = daemon.nagios_bin
203+
if daemon.running():
204+
c['status'] = 0
206205
c['friendly_status'] = "running"
207-
elif s == 1:
208-
c['friendly_status'] = "not running"
209206
else:
210-
c['friendly_status'] = 'unknown (exit status %s)' % (s, )
207+
c['status'] = 1
208+
c['friendly_status'] = "not running"
211209
needs_reload = pynag.Model.config.needs_reload()
212210
c['needs_reload'] = needs_reload
213211
return render_to_response('nagios_service.html', c, context_instance=RequestContext(request))

adagios/settings.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,9 @@
228228
# Anything put in /etc/adagios.d/adagios.conf will overwrite this.
229229
nagios_config = None # Sensible default is "/etc/nagios/nagios.cfg"
230230
nagios_url = "/nagios"
231-
nagios_init_script = "/etc/init.d/nagios"
231+
# define if you are using a sysv5 init script
232+
nagios_init_script = None
233+
nagios_service = "nagios"
232234
nagios_binary = "/usr/bin/nagios"
233235
livestatus_path = None
234236
livestatus_limit = 500

0 commit comments

Comments
 (0)