Skip to content

Commit

Permalink
nInput_Shell: +cmdEach, +parseYaml
Browse files Browse the repository at this point in the history
  • Loading branch information
nmsaguiar committed Aug 10, 2023
1 parent e6c9390 commit 3875ff5
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 61 deletions.
10 changes: 10 additions & 0 deletions config/inputs.disabled/yaml/10.getIOPerProc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
input:
name : Input java gc
cron : "*/2 * * * * *"
execFrom: nInput_Shell
execArgs:
cmdEach : "echo key: {{this}} && echo cmd: `cat /proc/{{this}}/cmdline` && cat /proc/{{this}}/io"
cmd : "find /tmp -type f | grep /tmp/hsperfdata_ | sed 's,.*/,,' | sed 's,^,- ,'"
eachParseYaml: true
parseYaml : true
attrTemplate : "Java processes/IO"
150 changes: 89 additions & 61 deletions config/objects/nInput_Shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,146 +14,174 @@
* \
* </odoc>
*/
var nInput_Shell = function(aCommand, isJson, attributeName) {
if (isMap(aCommand)) {
this.params = aCommand;
this.cmd = (isDef(aCommand.cmd) ? aCommand.cmd : "");
this.parseJson = (isDef(aCommand.parseJson) ? aCommand.parseJson : false);
this.parseYaml = (isDef(aCommand.parseYaml) ? aCommand.parseYaml : false)
this.name = (isDef(aCommand.name) ? aCommand.name : this.cmd);
this.attrTemplate = (isDef(aCommand.attrTemplate) ? aCommand.attrTemplate : "Server status/{{name}}");
} else {
this.cmd = aCommand;
this.parseJson = isJson;
this.name = "";
this.attrTemplate = (isUnDef(attributeName)) ? "Server status/" + aCommand : attributeName;
var nInput_Shell = function(params) {
if (isMap(params)) {
this.params = params
this.cmd = (isDef(params.cmd) ? params.cmd : "")
this.cmdEach = (isDef(params.cmdEach) ? params.cmdEach : "")
this.parseJson = (isDef(params.parseJson) ? params.parseJson : false)
this.parseYaml = (isDef(params.parseYaml) ? params.parseYaml : false)
this.eachParseJson = (isDef(params.eachParseJson) ? params.eachParseJson : false)
this.eachParseYaml = (isDef(params.eachParseYaml) ? params.eachParseYaml : false)
this.name = (isDef(params.name) ? params.name : this.cmd)
this.attrTemplate = (isDef(params.attrTemplate) ? params.attrTemplate : "Shell/{{name}}")
}

nInput.call(this, this.input);
nInput.call(this, this.input)
};
inherit(nInput_Shell, nInput);
inherit(nInput_Shell, nInput)

/**
*/
nInput_Shell.prototype.input = function(scope, args) {
var ret = {};
var ret = {}

// Generic parsing function
// Generic parsing functions
var _parse = s => {
if (this.params.parseJson) return jsonParse(s, true)
if (this.params.parseYaml) return af.fromYAML(s)
}
var _parseEach = s => {
if (this.params.eachParseJson) return jsonParse(s, true)
if (this.params.eachParseYaml) return af.fromYAML(s)
}

// Generic execution functions
var _exec = (fn, cmd, cmdEach) => {
if (isDef(cmdEach)) {
var lst = _parse(fn(cmd))
if (isArray(lst)) {
var _r = []
lst.forEach(v => {
lprint(templify(cmdEach, v))
var __r = _parseEach(fn(templify(cmdEach, v)))
_r.push(__r)
})
} else {
throw "nInput_Shell | Not a list/array (" + af.toSLON(lst) + ")"
}
return _r
} else {
return _parse(fn(cmd))
}
}

if (isDef(this.params.chKeys) || isDef(this.params.keys)) {
var res = [], attrname;
var parent = this;
var res = [], attrname
var parent = this

if (isArray(this.cmd)) this.cmd = this.cmd.join(" && ");
_$(this.cmd).isString().$_();
if (isArray(this.cmd)) this.cmd = this.cmd.join(" && ")
_$(this.cmd).isString().$_()

if (isDef(this.params.chKeys)) this.params.keys = $ch(this.params.chKeys).getKeys().map(r => r.key);
if (isDef(this.params.chKeys)) this.params.keys = $ch(this.params.chKeys).getKeys().map(r => r.key)

for(var i in this.params.keys) {
var v = $ch(this.params.chKeys).get({ key: this.params.keys[i] });
v = __nam_getSec(v);
var v = $ch(this.params.chKeys).get({ key: this.params.keys[i] })
v = __nam_getSec(v)

try {
switch(v.type) {
case "kube":
if (isUnDef(getOPackPath("Kube"))) {
throw "Kube opack not installed.";
throw "Kube opack not installed."
}

var s = $sec(v.secRepo, v.secBucket, v.secBucketPass, v.secMainPass, v.secFile);
var s = $sec(v.secRepo, v.secBucket, v.secBucketPass, v.secMainPass, v.secFile)
var k;
if (isDef(v.secObjKey)) {
var k = s.getObj(v.secObjKey);
var k = s.getObj(v.secObjKey)
}
if (isDef(v.secKey)) {
var ka = s.get(v.secKey);
k = new Kube(ka.url, ka.user, ka.pass, ka.wsTimeout, ka.token);
var ka = s.get(v.secKey)
k = new Kube(ka.url, ka.user, ka.pass, ka.wsTimeout, ka.token)
}
if (isUnDef(k) || isUnDef(k.getNamespaces)) {
throw "The secObjKey = '" + v.secObjKey + "' is not a valid Kube object.";
throw "The secObjKey = '" + v.secObjKey + "' is not a valid Kube object."
}

var epods = [];
var epods = []
if (isUnDef(v.pod)) {
if (isDef(v.podTemplate)) {
var pods = k.getPods(v.namespace);
var pods = k.getPods(v.namespace)
epods = $from(pods)
.equals("Kind", "Pod")
.match("Metadata.Name", v.podTemplate)
.select(r => r.Metadata.Name);
.select(r => r.Metadata.Name)
} else {
throw "No pod determined for '" + v.secObjKey + "'";
throw "No pod determined for '" + v.secObjKey + "'"
}
} else {
epods = [ v.pod ];
epods = [ v.pod ]
}

epods.forEach(pod => {
try {
var rr = String(k.exec(v.namespace, pod, [ templify(parent.cmd) ], void 0, true));
//var rr = String(k.exec(v.namespace, pod, [ templify(parent.cmd) ], __, true))
if (parent.parseJson || parent.parseYaml) {
res.push({
key: parent.params.keys[i],
result: _parse(rr)
result: _exec(c => {
return String(k.exec(v.namespace, pod, [ templify(c) ], __, true))
}, parent.cmd, parent.cmdEach)
});
} else {
res.push({
key: parent.params.keys[i],
result: rr
result: _exec(c => {
return String(k.exec(v.namespace, pod, [ templify(c) ], __, true))
}, parent.cmd, parent.cmdEach)
});
}
;
} catch(e) {
logErr("nInput_Shell | Error on namespace '"+ v.namespace + "', pod '" + pod + "': " + String(e));
logErr("nInput_Shell | Error on namespace '"+ v.namespace + "', pod '" + pod + "': " + String(e))
}
});
})

break;
break
case "ssh":
default:
nattrmon.useObject(this.params.keys[i], (ssh) => {
if (this.parseJson || this.parseYaml) {
res.push({
key: this.params.keys[i],
result: _parse(ssh.exec(templify(this.cmd)))
});
result: _exec(c => {
return ssh.exec(templify(c))
}, this.cmd, this.cmdEach)
})
} else {
res.push({
key: this.params.keys[i],
result: ssh.exec(templify(this.cmd))
});
result: _exec(c => {
return ssh.exec(templify(c))
}, this.cmd, this.cmdEach)
})
}
});
})
}
} catch(pke) {
logErr(" nInput_Shell | " + this.params.keys[i] + " | " + pke)
}
}

if (this.params.keys.length == 1) {
attrname = templify(this.attrTemplate, { name: this.name, key: this.params.keys[0]});
res = res[0].result;
attrname = templify(this.attrTemplate, { name: this.name, key: this.params.keys[0]})
res = res[0].result
} else {
attrname = templify(this.attrTemplate, { name: this.name });
attrname = templify(this.attrTemplate, { name: this.name })
}

ret[attrname] = res;
ret[attrname] = res
} else {
var attrname = templify(this.attrTemplate, { name: this.name });
var value = $sh(templify(this.cmd)).get(0);

value = (isDef(value.stdin) ? value.stdin : "") + (isDef(value.stdout) ? value.stdout : "");

if (this.parseJson || this.parseYaml) {
ret[attrname] = _parse(value)
} else {
ret[attrname] = value;
var attrname = templify(this.attrTemplate, { name: this.name })
var fn = c => {
var value = $sh(templify(c)).get(0)
value = (isDef(value.stdin) ? value.stdin : "") + (isDef(value.stdout) ? value.stdout : "")
return value
}

ret[attrname] = _exec(fn, this.cmd, this.cmdEach)
}

return ret;
};
return ret
}

0 comments on commit 3875ff5

Please sign in to comment.