Esempio n. 1
0
 def write_stats(self):
     with open(self.last_stats_file, "w") as ofile:
         json.dump(self.stats, ofile)
     if len(self.stats["targets"]) < 2:
         return
     self.log.info("total transfered %s at %s to %d targets",
                   print_size(self.stats["bytes"], unit="B"),
                   print_size(self.stats["speed"], unit="B") + "/s",
                   len(self.stats["targets"]))
Esempio n. 2
0
 def _update_stats(self, data, target=None):
     self.log.info("transfered %s at %s", print_size(data["bytes"],
                                                     unit="B"),
                   print_size(data["speed"], unit="B") + "/s")
     # aggregate stats
     self.stats["bytes"] += data["bytes"]
     n_targets = len(self.stats["targets"])
     self.stats["speed"] = (data["speed"] * n_targets +
                            data["speed"]) / (n_targets + 1)
     self.stats["targets"][target] = data
Esempio n. 3
0
 def load_free_total(key):
     if "monitor" not in data:
         return
     line = [
         colorize("  "+key, color.BOLD),
         "",
         "",
         "",
         "",
         "",
         "",
         "",
         "",
         "",
         "",
         "",
         "|" if nodenames else "",
     ]
     for nodename in nodenames:
         total = data["monitor"]["nodes"].get(nodename, {}).get("stats", {}).get(key+"_total")
         avail = data["monitor"]["nodes"].get(nodename, {}).get("stats", {}).get(key+"_avail")
         limit = 100 - data["monitor"]["nodes"].get(nodename, {}).get("min_avail_"+key, 0)
         if avail is None or total in (0, None):
             line.append(colorize("-", color.LIGHTBLUE))
             continue
         usage = 100 - avail
         total = print_size(total, unit="MB", compact=True)
         if limit:
             cell = "%d/%d%%:%s" % (usage, limit, total)
         else:
             cell = "%d%%:%s" % (usage, total)
         if usage > limit:
             cell = colorize(cell, color.RED)
         line.append(cell)
     out.append(line)
Esempio n. 4
0
 def _add_key(self, key, data):
     if not key:
         raise ex.Error("secret key name can not be empty")
     if data is None:
         raise ex.Error("secret value can not be empty")
     data = "crypt:" + base64.urlsafe_b64encode(
         self.encrypt(data, cluster_name="join", encode=True)).decode()
     self.set_multi(["data.%s=%s" % (key, data)])
     self.log.info("secret key '%s' added (%s)", key,
                   print_size(len(data), compact=True, unit="b"))
     # refresh if in use
     self.postinstall(key)
Esempio n. 5
0
    def print_dev_bottom_up(self,
                            chain=None,
                            node=None,
                            highlight=None,
                            verbose=False):
        if highlight is None:
            highlight = []
        if chain is None:
            chain = []
        if len(chain) == 0:
            prev_size = 0
            used_s = "-"
        else:
            prev_size = self.tree.get_dev(chain[-1].child).size
            used = chain[-1].get_used(prev_size)
            used_s = print_size(used)
        if prev_size == 0:
            pct = "-"
        else:
            pct = "%.2f%%" % (100 * used // self.size)

        node_dev = node.add_node()
        node_dev.add_column(self.alias, color.BROWN)
        node_dev.add_column(self.devtype)
        node_dev.add_column(used_s, align="right")
        node_dev.add_column(print_size(self.size), align="right")
        node_dev.add_column(pct, align="right")
        if verbose:
            col = node_dev.add_column()
            for devpath in self.devpath:
                if highlight is not None and devpath in highlight:
                    textcolor = color.LIGHTBLUE
                else:
                    textcolor = None
                col.add_text(devpath, textcolor)
        for r in self.parents:
            dev = self.get_dev(r.parent)
            dev.print_dev_bottom_up(chain + [r], node_dev, verbose=verbose)
Esempio n. 6
0
def fmt_mem_total(get, stats_data):
    if stats_data is None:
        return ""
    mem = 0
    for _data in stats_data.values():
        try:
            mem += get(_data)
        except (KeyError, TypeError) as exc:
            pass
    if mem == 0:
        return "     -"
    try:
        return print_size(mem, unit="b", compact=True)
    except Exception:
        return "     -"
Esempio n. 7
0
def fmt_blk(get, stats_data):
    if stats_data is None:
        return ""
    val = 0
    for _data in stats_data.values():
        try:
            val += get(_data)
        except (KeyError, TypeError) as exc:
            pass
    if val == 0:
        return "     -"
    try:
        return print_size(val, unit="b", compact=True)
    except Exception:
        return "     -"
Esempio n. 8
0
 def _add_key(self, key, data):
     if not key:
         raise ex.Error("configuration key name can not be empty")
     if data is None:
         raise ex.Error("configuration value can not be empty")
     if not is_string(data):
         data = "base64:" + bdecode(base64.urlsafe_b64encode(data))
     elif "\n" in data:
         data = "base64:" + bdecode(base64.urlsafe_b64encode(bencode(data)))
     else:
         data = "literal:" + data
     self.set_multi(["data.%s=%s" % (key, data)])
     self.log.info("configuration key '%s' added (%s)", key,
                   print_size(len(data), compact=True, unit="b"))
     # refresh if in use
     self.postinstall(key)
Esempio n. 9
0
def speed(get, prev_stats, stats):
    fmt = "%8s"
    if stats is None:
        return ""
    total = 0
    for node, _data in stats.items():
        try:
            curr = get(_data)
            prev = get(prev_stats[node])
            interval = _data["timestamp"] - prev_stats[node]["timestamp"]
            total += (curr - prev) / interval
        except Exception as exc:
            raise ValueError
    if total == 0:
        return fmt % "-"
    return fmt % (print_size(total, unit="b", compact=True) + "b/s")
Esempio n. 10
0
 def create_volume_locked(self):
     volume = factory("vol")(name=self.volname,
                             namespace=self.svc.namespace,
                             node=self.svc.node)
     if volume.exists():
         self.log.info("volume %s already exists", self.volname)
         data = volume.print_status_data(mon_data=True)
         if not data or "cluster" not in data:
             return volume
         if not self.svc.node.get_pool(volume.pool):
             raise ex.Error("pool %s not found on this node" % volume.pool)
         if self.svc.options.leader and volume.topology == "failover" and \
            (self.owned() or not self.claimed(volume)) and \
            data["avail"] != "up":
             cluster_avail = data["cluster"].get("avail")
             if cluster_avail is None:
                 self.log.info(
                     "no take over decision, we are leader but unknown cluster avail for volume %s",
                     self.volname)
             elif cluster_avail == "up":
                 self.log.info(
                     "volume %s is up on, peer, we are leader: take it over",
                     self.volname)
                 volume.action("takeover",
                               options={
                                   "wait": True,
                                   "time": 60
                               })
         return volume
     elif not self.svc.options.leader:
         self.log.info(
             "volume %s does not exist, we are not leader: wait its propagation",
             self.volname)
         self.wait_for_fn(
             lambda: volume.exists(), 10, 1,
             "non leader instance waited for too long for the volume to appear"
         )
         return volume
     self.check_configure_requirements()
     self.log.info(
         "create new volume %s (pool name: %s, pool type: %s, "
         "access: %s, size: %s, format: %s, shared: %s)", self.volname,
         self.pool, self.pooltype, self.access,
         print_size(self.size, unit="B",
                    compact=True), self.format, self.shared)
     return self._configure_volume(volume)
Esempio n. 11
0
 def get_mem_bytes(self):
     s = '0'
     source = self.s_default
     try:
         s = self.node.conf_get('node', 'mem_bytes')
         s = str(s / 1024 / 1024)
         source = self.s_config
     except (ex.OptNotFound, ex.RequiredOptNotFound):
         try:
             s = self._get_mem_bytes()
             source = self.s_probe
         except AttributeError:
             pass
     return {
         "title": "mem",
         "value": s,
         "source": source,
         "formatted_value": print_size(s)
     }
Esempio n. 12
0
    def print_dev(self,
                  relation=None,
                  node=None,
                  highlight=None,
                  verbose=False):
        if highlight is None:
            highlight = []
        if relation is None:
            parent_size = 0
        else:
            parent_size = self.get_dev(relation.parent).get_size()
        if parent_size == 0:
            pct = "-"
        else:
            pct = "%.2f%%" % (100 * self.size // parent_size)

        node_dev = node.add_node()
        node_dev.add_column(self.alias, color.BROWN)
        node_dev.add_column(self.devtype)
        node_dev.add_column(print_size(self.size), align="right")
        node_dev.add_column(pct, align="right")
        if verbose:
            col = node_dev.add_column()
            for devpath in self.devpath:
                if highlight is not None and devpath in highlight:
                    textcolor = color.LIGHTBLUE
                else:
                    textcolor = None
                col.add_text(devpath, textcolor)

        for r in self.children:
            d = self.get_dev(r.child)
            if d is None:
                node_unk = node_dev.add_node()
                node_unk.add_column("%s (unknown)" % r.child)
            else:
                d.print_dev(relation=r,
                            node=node_dev,
                            highlight=highlight,
                            verbose=verbose)
Esempio n. 13
0
 def create_volume_locked(self):
     volume = factory("vol")(name=self.volname,
                             namespace=self.svc.namespace,
                             node=self.svc.node)
     if volume.exists():
         self.log.info("volume %s already exists", self.volname)
         data = volume.print_status_data(mon_data=True)
         if not data or "cluster" not in data:
             return volume
         if not self.svc.node.get_pool(volume.pool):
             raise ex.Error("pool %s not found on this node" % volume.pool)
         if self.svc.options.leader and volume.topology == "failover" and \
            (self.owned() or not self.claimed(volume)) and \
            data["avail"] != "up":
             cluster_avail = data["cluster"].get("avail")
             if cluster_avail is None:
                 self.log.info(
                     "no take over decision, we are leader but unknown cluster avail for volume %s",
                     self.volname)
             elif cluster_avail == "up":
                 self.log.info(
                     "volume %s is up on, peer, we are leader: take it over",
                     self.volname)
                 volume.action("takeover",
                               options={
                                   "wait": True,
                                   "time": 60
                               })
         return volume
     elif not self.svc.options.leader:
         self.log.info(
             "volume %s does not exist, we are not leader: wait its propagation",
             self.volname)
         self.wait_for_fn(
             lambda: volume.exists(), 10, 1,
             "non leader instance waited for too long for the "
             "volume to appear")
         return volume
     self.log.info(
         "create new volume %s (pool name: %s, pool type: %s, "
         "access: %s, size: %s, format: %s, shared: %s)", self.volname,
         self.pool, self.pooltype, self.access,
         print_size(self.size, unit="B",
                    compact=True), self.format, self.shared)
     pool = self.svc.node.find_pool(poolname=self.pool,
                                    pooltype=self.pooltype,
                                    access=self.access,
                                    size=self.size,
                                    fmt=self.format,
                                    shared=self.shared)
     if pool is None:
         raise ex.Error("could not find a pool matching criteria")
     pool.log = self.log
     try:
         nodes = self.svc._get("DEFAULT.nodes")
     except ex.OptNotFound:
         nodes = None
     env = {}
     for mapping in pool.volume_env:
         try:
             src, dst = mapping.split(":", 1)
         except Exception:
             continue
         args = src.split(".", 1)
         val = self.svc.oget(*args)
         if val is None:
             raise ex.Error("missing mapped key in %s: %s" %
                            (self.svc.path, mapping))
         if is_string(val) and ".." in val:
             raise ex.Error(
                 "the '..' substring is forbidden in volume env keys: %s=%s"
                 % (mapping, val))
         env[dst] = val
     pool.configure_volume(volume,
                           fmt=self.format,
                           size=self.size,
                           access=self.access,
                           nodes=nodes,
                           shared=self.shared,
                           env=env)
     volume = factory("vol")(name=self.volname,
                             namespace=self.svc.namespace,
                             node=self.svc.node)
     return volume