2 ;; nagios-mode, an Emacs mode for Nagios <http://www.nagios.org/>
3 ;; configuration files.
5 ;; Copyright Michael Orlitzky
7 ;; http://michael.orlitzky.com/
9 ;; This program is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; http://www.fsf.org/licensing/licenses/gpl.html
28 (defcustom nagios-indent-level 2
29 "Number of spaces in one indentation (tab)."
30 :type 'integer :group 'nagios
37 (defun nagios-indent-line(&optional flag)
38 "Indents a line, taking nesting into account."
40 (nagios-indent-to (nagios-calculate-indent))
44 (defun nagios-beginning-of-line-pos()
45 ;; Return the point position corresponding to the beginning
46 ;; of the current line.
53 (defun nagios-end-of-line-pos()
54 ;; Return the point position corresponding to the end
55 ;; of the current line.
62 (defun nagios-point-offset()
63 ;; How far are we from the beginning of the line?
64 (- (point) (nagios-beginning-of-line-pos))
67 (defun nagios-first-char-offset()
68 ;; How far is the first character on this line
69 ;; from the beginning of the line?
72 (skip-chars-forward " \t")
76 (defun nagios-first-char-pos()
77 ;; What's the position of the first character on this line?
78 (+ (nagios-beginning-of-line-pos) (nagios-first-char-offset))
81 (defun nagios-indent-to(indent-column)
82 "Indent the current line to column indent-column."
83 ;; Store the point in orig-pos.
84 (let ((orig-point (point)))
86 ;; And store the offset of the first character (with respect to the
87 ;; beginning of the line) in orig-first-char-offset.
88 (let ((orig-first-char-offset (nagios-first-char-offset)))
90 ;; Delete any leading whitespace, and move the point to the
91 ;; beginning of the line.
92 (delete-region (nagios-beginning-of-line-pos) (nagios-first-char-pos))
95 ;; Now insert indent-column spaces.
96 (let ((indent-remaining indent-column))
97 (while (< 0 indent-remaining)
99 (setq indent-remaining (- indent-remaining 1)))
102 ;; The text on the current line just moved left/right some amount;
103 ;; call it text-delta. We want to move the point that same distance.
104 (let ((text-delta (- (nagios-first-char-offset) orig-first-char-offset)))
105 (goto-char (+ orig-point text-delta))
108 ;; The point should never wind up to the left of indent-column, so
109 ;; if it's there, move it over to indent-column.
110 (if (< (nagios-point-offset) indent-column)
111 (goto-char (+ (nagios-beginning-of-line-pos) indent-column))
118 (defun nagios-char-is-commented(pos)
119 "True if the character at position pos is commented, nil otherwise."
122 (re-search-backward "\\(#\\|;\\)" (nagios-beginning-of-line-pos) t)
126 (defun nagios-char-is-commented-and-valid(pos)
127 "True if the character at position pos is commented and non-nil.
131 (nagios-char-is-commented pos)
136 (defun nagios-last-opening-brace()
137 "Returns the position of the last opening brace, with
138 respect to the current point. Ignores braces which
141 (let ((lob (re-search-backward "{" nil t)))
143 (while (nagios-char-is-commented-and-valid lob)
145 (setq lob (re-search-backward "{" nil t))
156 (defun nagios-last-closing-brace()
157 "Get the position of the last closing brace, with
158 respect to the current point. Ignores braces which
161 (let ((lcb (re-search-backward "}" nil t)))
163 (while (nagios-char-is-commented-and-valid lcb)
165 (setq lcb (re-search-backward "}" nil t))
175 (defun nagios-in-block()
176 "Determine if the point is inside of a {} block."
178 ;; If the last brace seen in the buffer is an opening brace, we're
179 ;; in a block. Otherwise, we aren't.
180 (if (>= (nagios-last-closing-brace) (nagios-last-opening-brace))
186 (defun nagios-brace-on-line()
187 ;; Is there a curly brace on this line?
190 (re-search-forward "[{}]" (nagios-end-of-line-pos) t)
195 (defun nagios-calculate-indent()
196 "Calculate the level of indentation."
198 ;; We're either inside a block, or we aren't.
199 ;; Initialize the indent variable to either nagios-indent-level
200 ;; or 0 depending on whether or not we're in a block.
201 (let ((indent (if (nagios-in-block)
207 ;; Set the indentation level to 0 if we find either brace on this
209 (if (and (nagios-brace-on-line)
210 (not (nagios-char-is-commented (nagios-brace-on-line))))
220 (defun nagios-insert-right-brace-and-indent()
221 "Insert a '}' character, and indent the line."
228 (defvar nagios-mode-map()
229 "Keymap used in nagios mode.")
231 (when (not nagios-mode-map)
232 (setq nagios-mode-map (make-sparse-keymap))
233 (define-key nagios-mode-map
235 'nagios-insert-right-brace-and-indent)
240 (defconst nagios-directives
242 (concat "^[ \t\r\n]*"
245 '("active_checks_enabled"
257 "contactgroup_members"
261 "dependent_host_name"
262 "dependent_service_description"
265 "event_handler_enabled"
266 "execution_failure_criteria"
267 "failure_prediction_enabled"
269 "first_notification_delay"
270 "flap_detection_enabled"
271 "freshness_threshold"
273 "high_flap_threshold"
275 "host_notification_commands"
276 "host_notification_options"
277 "host_notification_period"
278 "host_notifications_enabled"
287 "normal_check_interval"
289 "notification_failure_criteria"
290 "notification_interval"
291 "notification_options"
292 "notification_period"
293 "notifications_enabled"
294 "obsess_over_service"
298 "passive_checks_enabled"
300 "retain_nonstatus_information"
301 "retain_status_information"
302 "retry_check_interval"
305 "service_description"
306 "service_notification_commands"
307 "service_notification_options"
308 "service_notification_period"
309 "service_notifications_enabled"
324 (defconst nagios-macros
364 "$CONTACTGROUPALIAS$"
365 "$CONTACTGROUPMEMBERS$"
367 "$CONTACTGROUPNAMES$"
373 "$HOSTACKAUTHORALIAS$"
374 "$HOSTACKAUTHORNAME$"
379 "$HOSTANDSERVICESIMPORTANCE$"
388 "$HOSTEXECUTIONTIME$"
389 "$HOSTGROUPACTIONURL$"
391 "$HOSTGROUPMEMBERADDRESSES$"
396 "$HOSTGROUPNOTESURL$"
403 "$HOSTNOTIFICATIONENABLED$"
404 "$HOSTNOTIFICATIONID$"
405 "$HOSTNOTIFICATIONNUMBER$"
406 "$HOSTNOTIFICATIONPERIOD$"
408 "$HOSTPERCENTCHANGE$"
419 "$LASTHOSTPROBLEMID$"
421 "$LASTHOSTSTATECHANGE$"
423 "$LASTHOSTUNREACHABLE$"
426 "$LASTSERVICECRITICAL$"
427 "$LASTSERVICEEVENTID$"
429 "$LASTSERVICEPROBLEMID$"
431 "$LASTSERVICESTATECHANGE$"
432 "$LASTSERVICESTATEID$"
433 "$LASTSERVICEUNKNOWN$"
434 "$LASTSERVICEWARNING$"
438 "$LONGSERVICEOUTPUT$"
441 "$MAXSERVICEATTEMPTS$"
443 "$NOTIFICATIONAUTHOR$"
444 "$NOTIFICATIONAUTHORALIAS$"
445 "$NOTIFICATIONAUTHORNAME$"
446 "$NOTIFICATIONCOMMENT$"
447 "$NOTIFICATIONISESCALATED$"
448 "$NOTIFICATIONNUMBER$"
449 "$NOTIFICATIONRECIPIENTS$"
454 "$RETENTIONDATAFILE$"
456 "$SERVICEACKAUTHORALIAS$"
457 "$SERVICEACKAUTHORNAME$"
458 "$SERVICEACKCOMMENT$"
461 "$SERVICECHECKCOMMAND$"
464 "$SERVICEDISPLAYNAME$"
467 "$SERVICEDURATIONSEC$"
469 "$SERVICEEXECUTIONTIME$"
470 "$SERVICEGROUPACTIONURL$"
471 "$SERVICEGROUPALIAS$"
472 "$SERVICEGROUPMEMBERS$"
474 "$SERVICEGROUPNAMES$"
475 "$SERVICEGROUPNOTES$"
476 "$SERVICEGROUPNOTESURL$"
477 "$SERVICEIMPORTANCE$"
479 "$SERVICEISVOLATILE$"
483 "$SERVICENOTIFICATIONENABLED$"
484 "$SERVICENOTIFICATIONID$"
485 "$SERVICENOTIFICATIONNUMBER$"
486 "$SERVICENOTIFICATIONPERIOD$"
488 "$SERVICEPERCENTCHANGE$"
490 "$SERVICEPERFDATAFILE$"
501 "$TOTALHOSTPROBLEMS$"
502 "$TOTALHOSTPROBLEMSUNHANDLED$"
504 "$TOTALHOSTSDOWNUNHANDLED$"
505 "$TOTALHOSTSERVICES$"
506 "$TOTALHOSTSERVICESCRITICAL$"
507 "$TOTALHOSTSERVICESOK$"
508 "$TOTALHOSTSERVICESUNKNOWN$"
509 "$TOTALHOSTSERVICESWARNING$"
510 "$TOTALHOSTSUNREACHABLE$"
511 "$TOTALHOSTSUNREACHABLEUNHANDLED$"
513 "$TOTALSERVICEPROBLEMS$"
514 "$TOTALSERVICEPROBLEMSUNHANDLED$"
515 "$TOTALSERVICESCRITICAL$"
516 "$TOTALSERVICESCRITICALUNHANDLED$"
518 "$TOTALSERVICESUNKNOWN$"
519 "$TOTALSERVICESUNKNOWNUNHANDLED$"
520 "$TOTALSERVICESWARNING$"
521 "$TOTALSERVICESWARNINGUNHANDLED$"
782 (defconst nagios-definitions
785 (concat "^[ \t\r\n]*"
787 "\\(" ;; Stick parenthesis around whatever comes out
788 ;; of regexp-opt. We use this to match a
789 ;; subexpression during font-lock.
793 "define contactgroup"
795 "define hostdependency"
796 "define hostescalation"
801 "define servicedependency"
802 "define serviceescalation"
803 "define serviceextinfo"
804 "define servicegroup"
805 "define timeperiod"))
806 ;; This closes the parentheses that we opened
807 "\\)" ;; before regexp-opt.
809 ;; These can be "terminated" by either an opening curly
810 ;; brace, or a space.
817 (defconst nagios-special
819 (concat "^[ \t\r\n]*"
822 '("name" "register" "use") t)
829 ;; The One True Font Locking Variable
831 (defvar nagios-font-lock-keywords
833 (cons nagios-special font-lock-keyword-face)
834 (cons nagios-directives font-lock-variable-name-face)
835 (cons nagios-macros font-lock-constant-face)
836 (cons nagios-definitions '(1 font-lock-function-name-face)))
838 "Rules for highlighting Nagios configuration files."
843 (defvar nagios-mode-syntax-table nil
844 "Syntax table used in nagios-mode buffers.")
845 (if nagios-mode-syntax-table
847 (setq nagios-mode-syntax-table (make-syntax-table))
848 (modify-syntax-entry ?# "< b" nagios-mode-syntax-table) ;; Comment style 1
849 (modify-syntax-entry ?\; "< b" nagios-mode-syntax-table) ;; Comment style 2
850 (modify-syntax-entry ?\n "> b" nagios-mode-syntax-table) ;; End comment
854 ;; Main Mode Function
857 "Major mode for editing Nagios configuration files."
860 (kill-all-local-variables)
861 (make-local-variable 'font-lock-defaults)
862 (make-local-variable 'comment-start)
863 (make-local-variable 'comment-start-skip)
864 (make-local-variable 'comment-end)
865 (make-local-variable 'indent-line-function)
867 (set-syntax-table nagios-mode-syntax-table)
869 (setq mode-name "nagios"
870 major-mode 'nagios-mode
871 indent-line-function 'nagios-indent-line
872 font-lock-defaults '(nagios-font-lock-keywords)
874 comment-start-skip "#\|; +"
879 (use-local-map nagios-mode-map)
881 ;; I don't /think/ I need to define this before attempting
882 ;; to run it. Users can define it if they want.
883 (run-hooks 'nagios-mode-hook)
887 (provide 'nagios-mode)