Skip to content

Instantly share code, notes, and snippets.

@jxy
Last active February 27, 2017 22:12
Show Gist options
  • Save jxy/c6064818b3b48b7018b8895c30e19cfe to your computer and use it in GitHub Desktop.
Save jxy/c6064818b3b48b7018b8895c30e19cfe to your computer and use it in GitHub Desktop.
qf (Qstat Filter) 1.0, Copyright (C) 2017 Xiao-Yong Jin, License MIT [https://opensource.org/licenses/MIT]
#!/bin/bash
help(){ cat;}<<'EOF'
qf (Qstat Filter) 1.0
Copyright (C) 2017 Xiao-Yong Jin
License MIT [https://opensource.org/licenses/MIT]
usage: qf [help | FILTERS]
Print out jobs satisfying all the filter categories specified.
A job satisfies a category, if it satisfies any filter in the category.
A job is not printed if it satisfies any filter prefixed by "-",
in STRING, State, or Queue categories.
Filter categories:
NRACK|NNODE An int number of racks 0-48, or nodes 512-49152
STRING Arbitrary regex
qrhf State: q:queued, r:running, h:hold, f:fail
cslbd Queue: c:capability, s:short, l:long, b:backfill, d:default
- Prefix to STRING and qrhfcsl, meaning "and not" matching
help This help
EOF
declare B='n=split("512|1024|2048|4096|8192|16384|24576|32768|49152",p,"|");p[0]=-1;' # Node list in awk
declare R='' # Regex matching
declare R0='' # Regex not matching
while (($#>0)); do
if [[ $1 =~ ^[0-9]+$ ]]; then
if (($1>=512&&$1<=49152)); then B+='x='$1' ;for(i=1;i<=n;++i){if(x<=p[i]&&x>p[i-1]){pn[++k]=i;break}}'
elif (($1>=0&&$1<=48)); then B+='x='$1'*1024;for(i=1;i<=n;++i){if(x<=p[i]&&x>p[i-1]){pn[++k]=i;break}}'
else help;exit 1
fi
else
case $1 in
(help) help;exit;;
( q) B+='S[++s]="queued";';;
( r) B+='S[++s]="running";';;
( h) B+='S[++s]="hold";';;
( f) B+='S[++s]="fail";';;
(-q) B+='S0[++s0]="queued";';;
(-r) B+='S0[++s0]="running";';;
(-h) B+='S0[++s0]="hold";';;
(-f) B+='S0[++s0]="fail";';;
( c) B+='Q[++q]="capability";';;
( s) B+='Q[++q]="short";';;
( l) B+='Q[++q]="long";';;
( b) B+='Q[++q]="backfill";';;
( d) B+='Q[++q]="default";';;
(-c) B+='Q0[++q0]="capability";';;
(-s) B+='Q0[++q0]="short";';;
(-l) B+='Q0[++q0]="long";';;
(-b) B+='Q0[++q0]="backfill";';;
(-d) B+='Q0[++q0]="default";';;
(-*) R0+="&&(!/$(printf '%s' "${1#-}"|sed 's,/,\\/,g')/)";;
( *) R+="|| /$(printf '%s' "$1" |sed 's,/,\\/,g')/" ;;
esac
fi
shift
done
[[ -n $R ]] && R="&&(${R#??})"
qstat --header=JobID:JobName:User:Project:Score:WallTime:QueuedTime:RunTime:Nodes:State:Queue \
|sed '2d;3,${s/ \([0-9.]*\) \(K\|M\) / \1\2 /;}' \
|awk ' function reqNodes (z){z=0;if(k==0)z=1;else for(i=1;i<=k;++i)if($9>p[pn[i]-1]&&$9<=p[pn[i]]){z=1;break}return z}
function reqStatus(z){z=0;if(s==0)z=1;else for(i=1;i<=s;++i)if($10~S[i]){z=1;break}if(s0>0)for(i=1;i<=s0;++i)if($10~S0[i]){z=0;break}return z}
function reqQueue (z){z=0;if(q==0)z=1;else for(i=1;i<=q;++i)if($11~Q[i]){z=1;break}if(q0>0)for(i=1;i<=q0;++i)if($11~Q0[i]){z=0;break}return z}
BEGIN{'"$B"'}NR==1{printf("#");print}NR>1'"$R0$R"'{if(reqNodes()&&reqStatus()&&reqQueue())print}' \
|column -t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment