Skip to content

Commit

Permalink
non key kube support
Browse files Browse the repository at this point in the history
  • Loading branch information
nmaguiar committed Aug 18, 2023
1 parent ed34a08 commit 278f895
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 9 deletions.
8 changes: 5 additions & 3 deletions config/inputs.disabled/yaml/12.shellProcCmd.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
input:
name : Input Java IO
cron : "*/2 * * * * *"
cron : "*/5 * * * * *"
execFrom: nInput_Shell
execArgs:
#type : local
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,^,- ,'"
cmdEach : |
echo "key: {{host}}:{{pid}}" && cat /proc/{{pid}}/io
cmd : |
find /tmp -type f -readable | grep /tmp/hsperfdata_ | sed "s,.*/,{'host':'$HOSTNAME'\,'pid':," | sed 's,^,- ,' | sed 's,$,},'
eachParseYaml: true
parseYaml : true
attrTemplate : "Java/IO"
Expand Down
73 changes: 67 additions & 6 deletions config/objects/nInput_Shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ nInput_Shell.prototype.input = function(scope, args) {
if (this.params.eachParseYaml) return af.fromYAML(s)
}

var setSec = aEntry => {
if (isDef(aEntry.secKey)) {
return __nam_getSec(aEntry)
} else {
return aEntry
}
}

// Generic execution functions
var _exec = (fn, cmd, cmdEach) => {
if (isDef(cmdEach)) {
Expand Down Expand Up @@ -203,13 +211,66 @@ nInput_Shell.prototype.input = function(scope, args) {
ret[attrname] = res
} else {
//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
switch(this.params.type) {
case "kube":
if (isUnDef(getOPackPath("Kube"))) throw "Kube opack not installed."
loadLib("kube.js")

this.params.kube = _$(this.params.kube, "kube").isMap().default({})

var m = setSec(this.params.kube)
traverse(m, (aK, aV, aP, aO) => {
if (isString(aV)) aO[aK] = templify(aV, m)
})
m.kind = _$(m.kind, "kube.kind").isString().default("FPO")
m.namespace = _$(m.namespace, "kube.namespace").isString().default("default")

var nss = m.namespace.split(/ *, */), lst = []

nss.forEach(ns => {
var its = $kube(m)["get" + m.kind](ns)
if (isMap(its) && isArray(its.items)) lst = lst.concat(its.items)
})

if (isMap(m.selector)) {
ow.obj.filter(lst, m.selector).forEach(r => {
var newM = clone(m)
traverse(newM, (aK, aV, aP, aO) => {
if (isString(aV)) aO[aK] = templify(aV, r)
})
})
}

ow.obj.filter(lst, m.selector).forEach(r => {
var newM = clone(m)
newM.pod = r.metadata.name
newM.namespace = r.metadata.namespace
try {
var fn = c => {
var res = nattrmon.shExec("kube", newM).exec(["/bin/sh", "-c", c])
if (isDef(res.stdout)) {
return res.stdout
} else {
return __
}
}

if (isUnDef(ret[attrname])) ret[attrname] = []
var _v = _posExec(_exec(fn, this.cmd, this.cmdEach))
if (isArray(_v)) ret[attrname] = ret[attrname].concat(_v); else ret[attrname].push(_v)
} catch(fe) { logErr("nInput_Shell | Kube " + newM.namespace + "::" + newM.pod + " | " + fe) }
})
break
case "local":
default :
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] = _posExec(_exec(fn, this.cmd, this.cmdEach))
}

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

return ret
Expand Down

0 comments on commit 278f895

Please sign in to comment.