Created
June 6, 2013 09:50
-
-
Save bjdean/5720467 to your computer and use it in GitHub Desktop.
Nagios2 command file updates with Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nagios2 | |
/********************************************************************* | |
* Nagios2 Command File access | |
* | |
* Copyright 2013 Bradley Dean | |
* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program. If not, see <http:www.gnu.org/licenses/>. | |
*/ | |
import ( | |
"os" | |
"fmt" | |
"time" | |
) | |
const ( | |
NAGIOS_CMD_FILE = "/var/lib/nagios2/rw/nagios.cmd" | |
) | |
type NagiosCmdFile struct { | |
Fh *os.File | |
} | |
func GetNagiosCmdFile () (ncmd *NagiosCmdFile, err error) { | |
fh, err := os.OpenFile( | |
NAGIOS_CMD_FILE, | |
os.O_RDWR, | |
0660, | |
) | |
// Abort (with error) if file open fails | |
if err != nil { | |
return | |
} | |
ncmd = new(NagiosCmdFile) | |
ncmd.Fh = fh | |
return | |
} | |
func (ncmd *NagiosCmdFile) Close () error { | |
return ncmd.Fh.Close() | |
} | |
func (ncmd *NagiosCmdFile) SendCommand (cmd string) error { | |
// Check if this is a regular file | |
fi, fi_err := ncmd.Fh.Stat() | |
if fi_err != nil { | |
return fi_err | |
} | |
is_regular_file := fi.Mode() & os.ModeType == 0 | |
// Seek to end (only if this is a regular file | |
if is_regular_file { | |
_, seek_err := ncmd.Fh.Seek(0, os.SEEK_END) | |
if seek_err != nil { | |
return seek_err | |
} | |
} | |
// TODO flock file, defer unlock | |
// Consider: func Flock(fd int, how int) (err error) | |
// Write command to file | |
_, printf_err := fmt.Fprintf(ncmd.Fh, "[%d] %s\n", time.Now().Unix(), cmd) | |
if printf_err != nil { | |
return printf_err | |
} | |
// Sync to disk (only if this is a regular file) | |
if is_regular_file { | |
sync_err := ncmd.Fh.Sync() | |
if sync_err != nil { | |
return sync_err | |
} | |
} | |
// Success | |
return nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nagios2 | |
/********************************************************************* | |
* Testing: Nagios2 Command File access | |
* | |
* Copyright 2013 Bradley Dean | |
* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program. If not, see <http:www.gnu.org/licenses/>. | |
*/ | |
import ( | |
"testing" | |
"io" | |
"fmt" | |
) | |
func TestGetNagiosCmdFile (t *testing.T) { | |
var ncmd *NagiosCmdFile | |
var err error | |
ncmd, err = GetNagiosCmdFile() | |
if err != nil { | |
t.Error("Received error:", err) | |
} | |
if ncmd == ncmd {} | |
} | |
func TestCloseNagiosCmdFile (t *testing.T) { | |
ncmd, err := GetNagiosCmdFile() | |
if err == nil { | |
defer ncmd.Close() | |
var emptyif interface {} | |
emptyif = ncmd | |
if _, ok := emptyif.(io.Closer); ! ok { | |
t.Error("ncmd is not a Closer") | |
} | |
} | |
} | |
func TestSendCommand (t *testing.T) { | |
ncmd, err := GetNagiosCmdFile() | |
if err == nil { | |
defer ncmd.Close() | |
var err error | |
err = ncmd.SendCommand("Test Command") | |
if err != nil { | |
t.Error("Failed to send command:", err) | |
} | |
} | |
} | |
func BenchmarkGetNagiosCmdFile (b *testing.B) { | |
ncmd, _ := GetNagiosCmdFile() | |
defer ncmd.Close() | |
} | |
func ExampleGetNagiosCmdFile () { | |
ncmd, err := GetNagiosCmdFile() | |
fmt.Printf("%T\n", ncmd) | |
fmt.Printf("%v\n", err) | |
// Output: | |
// *nagios2.NagiosCmdFile | |
// <nil> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment