| @@ 152-183 (lines=32) @@ | ||
| 149 | """Request and read stats from memcache socket.""" |
|
| 150 | stats = {} |
|
| 151 | ||
| 152 | if not self.socket and not self.connect(): |
|
| 153 | return {} |
|
| 154 | ||
| 155 | try: |
|
| 156 | if PY3: |
|
| 157 | self.socket.sendall(bytes(Memcache.STAT_CMD, 'utf8')) |
|
| 158 | else: |
|
| 159 | self.socket.sendall(Memcache.STAT_CMD) |
|
| 160 | ||
| 161 | st_str = self.socket.recv(Memcache.RECV_SIZE) |
|
| 162 | self.log.debug("Memcached: read: {0}".format(st_str)) |
|
| 163 | ||
| 164 | for line in st_str.split("\n"): |
|
| 165 | vals = line.split() |
|
| 166 | if len(vals) != 3: |
|
| 167 | continue |
|
| 168 | stype, sname, sval = vals |
|
| 169 | if stype == "STAT": |
|
| 170 | msg = "Memcached: {0} = {1}" |
|
| 171 | self.log.debug(msg.format(sname, sval)) |
|
| 172 | stats[sname] = sval |
|
| 173 | ||
| 174 | except Exception as e: |
|
| 175 | msg = "Memcached: {0}: poll: exception: {1}" |
|
| 176 | self.log.error(msg.format(self.addr, e)) |
|
| 177 | self.disconnect() |
|
| 178 | ||
| 179 | return stats |
|
| 180 | ||
| 181 | ||
| 182 | def connect(self): |
|
| 183 | """Connect to memcached, returns True if sucessful, False otherwise. |
|
| 184 | ||
| 185 | :rtype: bool |
|
| 186 | """ |
|
| @@ 212-242 (lines=31) @@ | ||
| 209 | sname = slave_str.format(i) |
|
| 210 | if sname not in stats: |
|
| 211 | break |
|
| 212 | try: |
|
| 213 | vals = stats[sname].split(",") |
|
| 214 | smetrics = dict((k,v) for k,v in (v.split('=') for v in vals)) |
|
| 215 | sip = smetrics['ip'].replace(".", "_") |
|
| 216 | smname = "{0}_{1}".format(sip, smetrics['port']) |
|
| 217 | ||
| 218 | # record offset and lag |
|
| 219 | mname = "{0}.slave.{1}.offset".format(name, sname) |
|
| 220 | soffset = moffset - int(smetrics['offset']) |
|
| 221 | result.add(plumd.Int(mname, soffset)) |
|
| 222 | mname = "{0}.slave.{1}.lag".format(name, sname) |
|
| 223 | result.add(plumd.Int(mname, smetrics['lag'])) |
|
| 224 | ||
| 225 | # if slave is online set online = 1, otherwise 0 |
|
| 226 | sonline = 1 if smetrics['state'] == "online" else 0 |
|
| 227 | mname = "{0}.slave.{1}.online".format(name, sname) |
|
| 228 | result.add(plumd.Int(mname, sonline)) |
|
| 229 | except(TypeError, KeyError, ValueError) as e: |
|
| 230 | self.log.error("Redis: invalid slave entry: {0}".format(sname)) |
|
| 231 | ||
| 232 | ||
| 233 | def get_metrics(self): |
|
| 234 | """Request and read stats from Redis socket.""" |
|
| 235 | stats = {} |
|
| 236 | ||
| 237 | if not self.socket and not self.connect(): |
|
| 238 | return {} |
|
| 239 | ||
| 240 | try: |
|
| 241 | if PY3: |
|
| 242 | self.socket.sendall(bytes(Redis.INFO_CMD, 'utf8')) |
|
| 243 | else: |
|
| 244 | self.socket.sendall(Redis.INFO_CMD) |
|
| 245 | ||