The %procid% sometimes blank in rsyslog template - rsyslog

I'm trying to configure rsyslog to output in RFC5424 format. This means that the PROCID must be output in the syslog header. If there's no header, it should output a single dash (-) in its place. However, some of the events output have it just blank, and some have an actual value.
This is rsyslogd 5.8.10 running on Amazon Linux.
Here are the config lines:
$template CustomFormat,"<%PRI%>1 %timegenerated:1:23:date-rfc3339%-00:00 %HOSTNAME% %app-name% b%procid%b %msgid% %STRUCTURED-DATA%%msg:::sp-if-no-1st-sp%%msg:::drop-last-lf%\n"
$ActionFileDefaultTemplate CustomFormat
Note that I put a "b" on each side of %procid% to make it more visible (this part is not RFC5424-compliant). Here are two lines of sample output.
<87>1 2019-06-19T20:03:01.929-00:00 ip-10-90-0-15 crond b29408b - - pam_unix(crond:account): expired password for user root (password aged)
<85>1 2019-06-19T20:17:18.150-00:00 ip-10-90-0-15 sudo bb - - ssm-user : TTY=pts/0 ; PWD=/ ; USER=root ; COMMAND=/bin/vi /etc/rsyslog.conf
The first line is correct, but the second example should have "b-b" instead of "bb". What should I do to make the blank %procid% show up as a dash? It works fine for the %msgid% and %STRUCTURED-DATA%.
Is there a better way to get RFC5424 output? (I have to use -00:00 instead of Z.)

There may be a better way, but one thing you can try is to use a Rainer script variable in the template instead of the property, and set this variable to "-" if the procid is empty. For example,
$template CustomFormat,"<%PRI%>1 ... b%$.myprocid%b ..."
$ActionFileDefaultTemplate CustomFormat
if ($procid == "") then {
set $.myprocid = "-";
} else {
set $.myprocid = $procid;
}
*.* ./outputfile
Just make sure the if statement is before any action statements. Note, you cannot change the procid property itself with set.

Related

How to write an ini file with if conditions

I want to write an ini file with if else conditions that i parse it with ConfigParser in python.
How to use if and Else statements in ini File ??
If I understand what you're asking...
What you're probably looking to do is something like this:
Set the value of the conditions in the INI file...
[section_1]
condition_a = true
condition_b = false
condition_c = false
Read in the conditions' boolean values using getboolean()...
import ConfigParser
config = ConfigParser()
config.read('my_file.ini')
if config.getboolean('section_1', 'condition_a'):
# Run your first conditional code
if config.getboolean('section_1', 'condition_b'):
# Run your second conditional code
if config.getboolean('section_1', 'condition_c'):
# Run your third conditional code

What is the Proper Method of Assigning JSON within a Ruby Variable?

The following JSON is a transaction what will be sent to the Ripple Network to query accounts that hold cryptographic assets at a Gateway (somewhat like a bank, more like a trust account between its clients). This script is to be used in conjunction with PHP to fetch a Gateway's issued balances and ignored it's hot-wallet or day-to-day operations wallet. My question is what is the proper way to:
a. Assign JSON within a Ruby variable?
b. What is the best way to escape double quotes and deal with newlines where brackets and square brackets occur within the JSON syntax?
The JSON follows:
ripple_path="/home/rippled/build/rippled"
conf = "--conf /etc/rippled/rippled.cfg"
puts "About to set the JSON lines "
gatewayStart = "\"method\": \"gateway_balances\","
paramsLine = "\"params\": [ {"
accountLine = "\"account\": \"rGgS5Hw3PhSp3VNT43PDTXze9YfdthHUH\","
hotwalletLine = "\"hotwallet\": \"rKYNhsT3aLymkGH7WL7ZUHkm6RE27iuM4C\","
liLine = "\"ledger_index\": \"validated\","
strictLine = "\"strict\": "
trueLine = true
endLine = " } ] }"
balancesLine = "#{gatewayStart} #{paramsLine} #{accountLine} #>{hotwalletLine} #{liLine} #{strictLine} #{trueLine} #{endLine}"
lineString = "#{balancesLine.to_s}"
linetoJSON = "#{lineString}"
puts "linetoJSON: #{linetoJSON} "
cmd2=`#{ripple_path} #{conf} json gateway_balances #{linetoJSON}`
cmder="#{ripple_path} #{conf} json gateway_balances #{linetoJSON}"
puts "Done."
The output is:
root#xagate:WorkingDirectory# ruby gatewaybal.rb
About to set the JSON lines
linetoJSON: "method": "gateway_balances", "params": [ { "account":
"rGgS5Hw3PhSp3VNT43PDTXze9YfdthHUH", "hotwallet": "rKYNhsT3aLymkGH7WL7ZUHkm6RE27iuM4C", "ledger_index": "validated", "strict":rue } ] }
Loading: "/etc/rippled/rippled.cfg"
rippled [options] <command> <params>
General Options:
-h [ --help ] Display this message.
.....
Done.
It is noteworthy that this command also returns a badSyntax error when executed manually via the command line. Please see here for the mirror of this issue raised on the ripple forums.
jsonLine = "'{ \"account\": \"rGgS5Hw3PhSp3VNT43PDTXze9YfdthHUH\", \"hotwallet\": \"rKYNhsT3aLymkGH7WL7ZUHkm6RE27iuM4C\", \"ledger_index\": \"validated\", \"strict\": true }'"
Is the proper way to assign this JSON within a single variable; this solution was provided by JoelKatz. The completed code is now available on GitHub.

issue with two nested loops in bash

I am trying to loop over two series of variables and make them with R statistical software to get the correlation results.
I got confused why the bigger loop (on mica_headers) doesn't take place while the inner loop finishes each time.
#!/bin/sh
#set -e
micaHeaderList="tot_instruction ILP32 ILP64 ILP128 ILP256 total_ins_count_for_hpc_alignment totInstruction mem-read mem-write control-flow arithmetic floating-point stack shift string sse other nop InstrFootprint64 InstrFootprint4k DataFootprint64 DataFootprint4k mem_access memReuseDist0-2 memReuseDist2-4 memReuseDist4-8 memReuseDist8-16 memReuseDist16-32 memReuseDist32-64 memReuseDist64-128 memReuseDist128-256 memReuseDist256-512 memReuseDist512-1k memReuseDist1k-2k memReuseDist2k-4k memReuseDist4k-8k memReuseDist8k-16k memReuseDist16k-32k memReuseDist32k-64k memReuseDist64k-128k memReuseDist128k-256k memReuseDist256k-512k memReuseDist512k-00 GAg_mispred_cnt_4bits PAg_mispred_cnt_4bits GAs_mispred_cnt_4bits PAs_mispred_cnt_4bits GAg_mispred_cnt_8bits PAg_mispred_cnt_8bits GAs_mispred_cnt_8bits PAs_mispred_cnt_8bits GAg_mispred_cnt_12bits PAg_mispred_cnt_12bits GAs_mispred_cnt_12bits PAs_mispred_cnt_12bits total_brCount total_transactionCount total_takenCount total_num_ops instr_reg_cnt total_reg_use_cnt total_reg_age reg_age_cnt_1 reg_age_cnt_2 reg_age_cnt_4 reg_age_cnt_8 reg_age_cnt_16 reg_age_cnt_32 reg_age_cnt_64 mem_read_cnt mem_read_local_stride_0 mem_read_local_stride_8 mem_read_local_stride_64 mem_read_local_stride_512 mem_read_local_stride_4096 mem_read_local_stride_32768 mem_read_local_stride_262144 mem_read_global_stride_0 mem_read_global_stride_8 mem_read_global_stride_64 mem_read_global_stride_512 mem_read_global_stride_4096 mem_read_global_stride_32768 mem_read_global_stride_262144 mem_write_cnt mem_write_local_stride_0 mem_write_local_stride_8 mem_write_local_stride_64 mem_write_local_stride_512 mem_write_local_stride_4096 mem_write_local_stride_32768 mem_write_local_stride_262144 mem_write_global_stride_0 mem_write_global_stride_8 mem_write_global_stride_64 mem_write_global_stride_512 mem_write_global_stride_4096 mem_write_global_stride_32768 mem_write_global_stride_262144"
mhToBeReplaced="ILP32"
compilerOptionList="funsafe_math_optimizations fno_guess_branch_probability fno_ivopts fno_tree_loop_optimize fno_inline_functions funroll_all_loops fno_omit_frame_pointer falign_jumps fselective_scheduling fno_inline_small_functions fno_tree_pre ftracer fno_move_loop_invariants"
coToBeReplaced="fno_guess_branch_probability"
for mica_header in $micaHeaderList
do
for compiler_option in $compilerOptionList
do
echo "Calculating $compiler_option correlation for $mica_header"
sed -i "s/$coToBeReplaced/$compiler_option/g" r.scr
coToBeReplaced=$compiler_option
make
done
sed -i "s/$mhToBeReplaced/$mica_header/g" r.scr
mhToBeReplaced=$mica_header
done
Edited: Generally, in order to avoid inconsistency between the values of iterations and source file, how could I be able to link these two together from different files. i.e. micaHeaderList with the headers inside the ALL.scv file ?
In the script there is a mistake which echoes a value of $mica_header which will be substituted only for make calls in the next outer loop run. This is caused by the second sed being run only after make. It should have been called before the inner loop. Could not this cause the confusion?
Also instead of replacing one value of a parameter for another it is better to create a template of r.src file with placeholders for the parameters to be changed. This way we can be sure that for some values of the parameters there could not be conflicts with the search patterns and that a typo will not prevent substitution.
Below is an improved script with considerably simpler code. The part inside if test "$Test" = 1 is just a testing code activated by Test=1. For this script just create a template for r.scr named r.scr.template and put the placeholders %%MicaHeader%% and %%CompilerOption%% instead of the parameters which should be replaced. For the first test there is a shorter list of parameters.
#!/bin/sh
micaHeaderList="tot_instruction ILP32 ILP64"
mhToBeReplaced=%%MicaHeader%%
compilerOptionList="funsafe_math_optimizations fno_guess_branch_probability fno_ivopts fno_tree_loop_optimize"
coToBeReplaced=%%CompilerOption%%
TemplateFile=r.scr.template
OutputFile=r.scr
Test=1
if test "$Test" = 1
then
TemplateFile="${TemplateFile}-test"
echo "$coToBeReplaced $mhToBeReplaced" >"$TemplateFile"
make ()
{
echo -n ">>> make: "
cat "$OutputFile"
}
fi
for mica_header in $micaHeaderList
do
for compiler_option in $compilerOptionList
do
echo "Calculating $compiler_option correlation for $mica_header"
sed "s/$mhToBeReplaced/$mica_header/g;s/$coToBeReplaced/$compiler_option/g" "$TemplateFile" >"$OutputFile"
make
done
done
If the problem still persists:
Check the generated r.src files.
Check if the make script really uses the substituted parameters from r.src.

Invalid command name in Tcl when using format

I'm running the following commands
set full_pin_name [format "%s %s" $top_cell $encoded_name]
puts "full_pin_name is $full_pin_name"
and I get the following error:
full_pin_name is invalid command name "A" B
when top_cell equals A and encoded_name equals B.
Why is this happening?
I suspect the problem is with your $top_cell variable, which has the value invalid command name "A". To check, try the following line before your format line:
puts ">$top_cell<"
If indeed, $top_cell value has a problem, you can then trace back to the last set command. Let us know if it fixes your problem, or we might try some other approaches.
Try repeating this in a plain Tcl shell, it works. Something is different in your Tcl shell.
Try
info body set
and
info body format
If either report something other than set isn't a procedure or format isn't a procedure then you have your culprit.
May be this code runs in a namespace which has the set command defined in it?
To demonstrate:
% namespace eval foo {
proc set args {
puts hey!
}
proc whatever {top_cell encoded_name} {
set full_pin_name [format "%s %s" $top_cell $encoded_name]
puts "full_pin_name is $full_pin_name"
}
}
% ::foo::whatever A B
hey!
can't read "full_pin_name": no such variable
%
As far as I can tell without a TCL interpreter here it should work assuming a normal interpreter with format defined. so it looks like maybe format has been renamed or doesn't exist as a command in the interpreter you are using?
To get you going you don't really need format to join strings anyway
set fill_pin_name "$top_cell $encoded_name"
should so what you need
Maybe your code looks like this:
set encoded_name B
if { [catch {[A]} top_cell]} {
set full_pin_name [format "%s %s" $top_cell $encoded_name]
puts "full_pin_name is $full_pin_name"
}
The result is:
full_pin_name is invalid command name "A" B

Why is my script not going up one level when using /../?

I have the understanding that using "/../" in a file location in bash will go up one level. That's what I'm trying to do here:
for box in {0..4}
do
for lvl in {0..24}
do
key="UNLOCKED_${box}_$lvl"
plutil -key "$key" -value '1' "$appdir/../Library/Preferences/com.chillingo.cuttherope.plist" 2>&1> /dev/null
#successCheck=$(plutil -key "$key" "/$appdir/../Library/Preferences/com.chillingo.cuttherope.plist")
#if [ "$successCheck" -lt 1 ]; then
# echo "Level ${box}-$lvl failed! "
#fi
done
done
But I keep getting this error (x125):
Error: File not found at path /var/mobile/Applications/1E17CC78-AA6E-4FFA-B241-74A73FE3AB0E/CutTheRope.app/../Library/Preferences/com.chillingo.cuttherope.plist
Any help would be greatly appreciated/
Thanks.
/ means start from the root of the filesystem or context, and ../ means go up one level. /../ effectively cancels the two out.
To go up one level, try ../
EDIT: In other words, try removing the / from the end of the variable in your path.
EDIT #2: The best solution is to probably just remove the /../
EDIT #3:
This would work:
/var/mobile/Applications/1E17CC78-AA6E-4FFA-B241-74A73FE3AB0E/CutTheRope.app/Library/../Library/Preferences/
And if you removed the second Library and the .. it should also work:
/var/mobile/Applications/1E17CC78-AA6E-4FFA-B241-74A73FE3AB0E/CutTheRope.app/Library/Preferences/
The lookup procedure is as follows:
inode lookup(string path) {
inode cur = path[0] == '/' ? process->root_directory : process->current_directory;
foreach component in split(path, '/') {
inode next = cur.get_entry(component);
if (next is a file && the component is the last one)
return;
if (next is a directory)
cur = next;
else
return null;
}
return cur;
}
There is no special handling for ... The point is that a file can only appear as the very last component of the path. If it appears before, the return null path will be taken.
The actual implementation (for example in NetBSD) is a bit more complicated.
Update: I just saw that in your question $appdir will probably point to a directory, so the above discussion does not apply. Still, there might be some symlinks involved. When you follow a symlink and then continue with .., the lookup function continues from where the symlink points to. A small example:
$ ln -s somewhere/else/deep/in/the/path symlink
$ ls -l symlink/..
This will resolve to somewhere/else/deep/in/the (provided it exists). The lookup function will not simplify the path symlink/.. to . or as another example first/../second will not be simplified to second.

Resources