]>
Commit | Line | Data |
---|---|---|
1 | #include "EXTERN.h" | |
2 | #include "perl.h" | |
3 | #include "XSUB.h" | |
4 | ||
5 | #include "ppport.h" | |
6 | ||
7 | #include <rados/librados.h> | |
8 | ||
9 | #define DEBUG_RADOS 0 | |
10 | ||
11 | #define DPRINTF(fmt, ...)\ | |
12 | do { if (DEBUG_RADOS) { printf("debug: " fmt, ## __VA_ARGS__); } } while (0) | |
13 | ||
14 | MODULE = PVE::RADOS PACKAGE = PVE::RADOS | |
15 | ||
16 | rados_t | |
17 | pve_rados_create(user) | |
18 | SV *user | |
19 | PROTOTYPE: $ | |
20 | CODE: | |
21 | { | |
22 | char *u = NULL; | |
23 | rados_t clu = NULL; | |
24 | ||
25 | if (SvOK(user)) { | |
26 | u = SvPV_nolen(user); | |
27 | } | |
28 | ||
29 | int ret = rados_create(&clu, u); | |
30 | ||
31 | if (ret == 0) | |
32 | RETVAL = clu; | |
33 | else { | |
34 | die("rados_create failed - %s\n", strerror(-ret)); | |
35 | RETVAL = NULL; | |
36 | } | |
37 | } | |
38 | OUTPUT: RETVAL | |
39 | ||
40 | void | |
41 | pve_rados_conf_set(cluster, key, value) | |
42 | rados_t cluster | |
43 | char *key | |
44 | char *value | |
45 | PROTOTYPE: $$$ | |
46 | CODE: | |
47 | { | |
48 | DPRINTF("pve_rados_conf_set %s = %s\n", key, value); | |
49 | ||
50 | int res = rados_conf_set(cluster, key, value); | |
51 | if (res < 0) { | |
52 | die("rados_conf_set failed - %s\n", strerror(-res)); | |
53 | } | |
54 | } | |
55 | ||
56 | void | |
57 | pve_rados_conf_read_file(cluster, path) | |
58 | rados_t cluster | |
59 | SV *path | |
60 | PROTOTYPE: $$ | |
61 | CODE: | |
62 | { | |
63 | char *p = NULL; | |
64 | ||
65 | if (SvOK(path)) { | |
66 | p = SvPV_nolen(path); | |
67 | } | |
68 | ||
69 | DPRINTF("pve_rados_conf_read_file %s\n", p); | |
70 | ||
71 | int res = rados_conf_read_file(cluster, p); | |
72 | if (res < 0) { | |
73 | die("rados_conf_read_file failed - %s\n", strerror(-res)); | |
74 | } | |
75 | } | |
76 | ||
77 | void | |
78 | pve_rados_connect(cluster) | |
79 | rados_t cluster | |
80 | PROTOTYPE: $ | |
81 | CODE: | |
82 | { | |
83 | DPRINTF("pve_rados_connect\n"); | |
84 | ||
85 | int res = rados_connect(cluster); | |
86 | if (res < 0) { | |
87 | die("rados_connect failed - %s\n", strerror(-res)); | |
88 | } | |
89 | } | |
90 | ||
91 | void | |
92 | pve_rados_shutdown(cluster) | |
93 | rados_t cluster | |
94 | PROTOTYPE: $ | |
95 | CODE: | |
96 | { | |
97 | DPRINTF("pve_rados_shutdown"); | |
98 | rados_shutdown(cluster); | |
99 | } | |
100 | ||
101 | SV * | |
102 | pve_rados_mon_command(cluster, cmds) | |
103 | rados_t cluster | |
104 | AV *cmds | |
105 | PROTOTYPE: $$ | |
106 | CODE: | |
107 | { | |
108 | const char *cmd[64]; | |
109 | size_t cmdlen = 0; | |
110 | ||
111 | char *outbuf =NULL; | |
112 | size_t outbuflen = 0; | |
113 | char *outs = NULL; | |
114 | size_t outslen = 0; | |
115 | ||
116 | SV *arg; | |
117 | ||
118 | while ((arg = av_pop(cmds)) && (arg != &PL_sv_undef)) { | |
119 | if (cmdlen >= 63) { | |
120 | die("too many arguments"); | |
121 | } | |
122 | cmd[cmdlen] = SvPV_nolen(arg); | |
123 | DPRINTF("pve_rados_mon_command%zd %s\n", cmdlen, cmd[cmdlen]); | |
124 | cmdlen++; | |
125 | } | |
126 | ||
127 | int ret = rados_mon_command(cluster, cmd, cmdlen, | |
128 | NULL, 0, | |
129 | &outbuf, &outbuflen, | |
130 | &outs, &outslen); | |
131 | ||
132 | if (ret < 0) { | |
133 | char msg[4096]; | |
134 | snprintf(msg, sizeof(msg), "mon_command failed - %s\n", outs); | |
135 | rados_buffer_free(outs); | |
136 | die(msg); | |
137 | } | |
138 | ||
139 | RETVAL = newSVpv(outbuf, outbuflen); | |
140 | ||
141 | rados_buffer_free(outbuf); | |
142 | } | |
143 | OUTPUT: RETVAL | |
144 | ||
145 | HV * | |
146 | pve_rados_cluster_stat(cluster) | |
147 | rados_t cluster | |
148 | PROTOTYPE: $ | |
149 | CODE: | |
150 | { | |
151 | struct rados_cluster_stat_t result; | |
152 | ||
153 | DPRINTF("pve_rados_cluster_stat"); | |
154 | ||
155 | int ret = rados_cluster_stat(cluster, &result); | |
156 | ||
157 | if(ret != 0) { | |
158 | warn("rados_cluster_stat failed (ret=%d)\n", ret); | |
159 | XSRETURN_UNDEF; | |
160 | } | |
161 | HV * rh = (HV *)sv_2mortal((SV *)newHV()); | |
162 | ||
163 | (void)hv_store(rh, "kb", 2, newSViv(result.kb), 0); | |
164 | (void)hv_store(rh, "kb_used", 7, newSViv(result.kb_used), 0); | |
165 | (void)hv_store(rh, "kb_avail", 8, newSViv(result.kb_avail), 0); | |
166 | (void)hv_store(rh, "num_objects", 11, newSViv(result.num_objects), 0); | |
167 | ||
168 | RETVAL = rh; | |
169 | } | |
170 | OUTPUT: RETVAL |