diff --git a/rados/conn.go b/rados/conn.go index 0bc4bcd..cdae3d6 100644 --- a/rados/conn.go +++ b/rados/conn.go @@ -259,3 +259,42 @@ func (c *Conn) DeletePool(name string) error { return RadosError(ret) } } + +// MonCommand sends a command to one of the monitors +func (c *Conn) MonCommand(args []byte) (buffer []byte, info string, err error) { + argv := make([]*C.char, len(args)) + for i, _ := range args { + argv[i] = (*C.char)(unsafe.Pointer(&args[i])) + } + + var ( + outs, outbuf *C.char + outslen, outbuflen C.size_t + ) + inbuf := C.CString("") + defer C.free(unsafe.Pointer(inbuf)) + + ret := C.rados_mon_command(c.cluster, + &argv[0], C.size_t(len(args)), + inbuf, // bulk input (e.g. crush map) + C.size_t(0), // length inbuf + &outbuf, // buffer + &outbuflen, // buffer length + &outs, // status string + &outslen) + + if outslen > 0 { + info = C.GoStringN(outs, C.int(outslen)) + C.free(unsafe.Pointer(outs)) + } + if outbuflen > 0 { + buffer = C.GoBytes(unsafe.Pointer(outbuf), C.int(outbuflen)) + C.free(unsafe.Pointer(outbuf)) + } + if ret != 0 { + err = RadosError(int(ret)) + return nil, info, err + } + + return +} diff --git a/rados/rados_test.go b/rados/rados_test.go index 82b05f0..26cf689 100644 --- a/rados/rados_test.go +++ b/rados/rados_test.go @@ -12,6 +12,7 @@ import "time" import "net" import "fmt" import "sort" +import "encoding/json" func GetUUID() string { out, _ := exec.Command("uuidgen").Output() @@ -375,6 +376,25 @@ func TestGetPoolName(t *testing.T) { conn.Shutdown() } +func TestMonCommand(t *testing.T) { + conn, _ := rados.NewConn() + conn.ReadDefaultConfigFile() + conn.Connect() + + command, err := json.Marshal(map[string]string{"prefix": "df", "format": "json"}) + assert.NoError(t, err) + + buf, info, err := conn.MonCommand(command) + assert.NoError(t, err) + assert.Equal(t, info, "") + + var message map[string]interface{} + err = json.Unmarshal(buf, &message) + assert.NoError(t, err) + + conn.Shutdown() +} + func TestObjectIterator(t *testing.T) { conn, _ := rados.NewConn() conn.ReadDefaultConfigFile()