How to change timestamp format on rsyslog - datetime

I want to change the default timestamp format on rsyslog. Currently I am running on RHEL7.2. Syslog version 7.4.7. Now the default format is the following:
Mar 23 09:35:30 localhost DEB [9125:<console>.<module>:2] debug info
Inside rsyslog.conf I have define the following template:
$template Mytemplate,"%timegenerated% %HOSTNAME% %syslogseverity-text:0:3:uppercase% %msg%\n"
How can I change the format to this YYYY-MM-dd H:i:s ?

The modification is the following:
$template Mytemplate,"%$year%-%$month%-%$day% %timegenerated:12:19:date-rfc3339% %HOSTNAME% %syslogseverity-text:0:3:uppercase% %msg%\n"

Related

how to convert yyyy-mm-dd hh:mm:ss to mm-dd-yyyy hh:mm am/pm in react native

I have a string as 2018-10-15 11:40:39 and I have to convert this into a string like 10-15-2018 11:40 AM. i'm beginner in react native.
From the root of your project just run:
npm install moment --save
Then you can import it in your code:
import moment from 'moment';
For date convert, use below code
console.log( moment('2018-01-01 13:40:39').format('MM-DD-YYYY hh:mm a') );
alert( moment('2018-01-01 13:40:39').format('MM-DD-YYYY hh:mm a') );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

Salt: text file to variable and use the same variable in state file to find&replace

I've run into an issue I havent been able to solve:
I have a file(/etc/osci) that I use on all of my servers as an name for our monitoring(zabbix)
I've created a state file that pushes a template configuration file to the server and and reads the content of /etc/osci to a variable. The next step would be to use that same variable with 'file.replace' function to search for a string and replace it with the variable.
uusnimi=$(cat /etc/osci):
cmd.run
/etc/zabbix_agentd.conf:
file.managed:
- source: salt://base/streamingconf/zabbix/zabbix_agentd.conf
- mode: 644
change_hostname_zabbix:
file.replace:
- name: /etc/zabbix_agentd.conf
- pattern: 'Hostname='
- repl: 'Hostname=$uusnimi'
Now when executing the state file echoing the variable in the target server it provides me the right output:
echo $uusnimi
Server1
but for the life of me I can't figure out how to escape the last line of the above code so it would insert the value not the '$uusnimi' string
Use uusnimi as a jinja variable.
{% set uusnimi = salt['cmd.shell']('cat /etc/osci') %}
/etc/zabbix_agentd.conf:
file.managed:
- source: salt://base/streamingconf/zabbix/zabbix_agentd.conf
- mode: 644
change_hostname_zabbix:
file.replace:
- name: /etc/zabbix_agentd.conf
- pattern: 'Hostname='
- repl: 'Hostname={{ uusnimi }}'

PHP setlocale doesn't work

I try to set the locale from fr_FR to us_US in PHP (php-fpm) with nginX on my raspberry-pi.
Here is my code:
<?php
system('locale -a');
/*
C POSIX en_US.utf8 fr_FR fr_FR.iso88591 fr_FR.iso885915#euro fr_FR.utf8
So the en_US locale is well installed
*/
echo locale_get_default();
/* fr_FR */
echo setlocale(LC_ALL, '0');
/*
LC_CTYPE=fr_FR.UTF-8;LC_NUMERIC=C;LC_TIME=C;LC_COLLATE=C;LC_MONETARY=C;
LC_MESSAGES=C;LC_PAPER=C;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;
LC_MEASUREMENT=C;LC_IDENTIFICATION=C
*/
var_dump(setlocale(LC_ALL, 'en_US.UTF-8', 'en_US.UTF8'));
/* bool(false) */
var_dump(setlocale(LC_ALL, 'fr_FR.UTF8'));
/* string(10) "fr_FR.UTF8"
useless, but it shows that setlocale works with the already set locale */
?>
I don't understand why setlocale returns false.
It seems something doesn't work but I don't manage to find what.
I had the same problem after installing additional locales on debian machine running nginx with php5-fpm
To fix the problem I had to restart php5-fpm.
Worked for me after I have ran:
sudo apt-get -y install language-pack-pt
service php7.1-fpm restart
<?php
date_default_timezone_set('America/Sao_Paulo');
setlocale(LC_ALL, 'pt_BR', 'pt_BR.iso88591', 'pt_BR.utf8');
echo \Carbon\Carbon::now()->addMonth()->formatLocalized('%d %B %Y');
From the docs (note On Windows):
On Windows, setlocale(LC_ALL, '') sets the locale names from the
system's regional/language settings (accessible via Control Panel).
http://php.net/manual/en/function.setlocale.php

Unix: Using date in alias doesn't output the current date/time

In my bash_profile, I'm referencing an external alias.sh file, which has the following alias:
alias date="echo `date "+%Y-%m-%d at %H:%M":%S`"
If I issue date in a new terminal session, it constantly outputs the specific date at the time when the alias.sh file was sourced (i.e. when I started the new session) …
How do I make an alias that actually outputs the current date, when executing the aliased command?
Uhh, why not just?
alias date='date "+%Y-%m-%d at %H:%M":%S'
No need to echo it. When you use the backwards tick ( ` ), whatever's in it gets evaluated when alias.sh is sourced.
you can place a backslash character in front of each backquote character. :
sysadmin#localhost:~$ alias p=\'date\'
sysadmin#localhost:~$ p
Tue Apr 4 13:17:57 UTC 2017
sysadmin#localhost:~$ echo Today is $(p)
Today is Tue Apr 4 13:19:18 UTC 2017
Add this to your ~/.aliases file:
alias cd 'cd \!* ; set prompt = "\n`/bin/pwd`\n:%{^[];%~^G%}%{^[[0;37m%}(%{^[[1;36m%}%p%{^[[0;37m%})%{^[[0;35m%}%m%{^[[0;37m%}%{^[[0;33m%}>%{^[[0;37;37m%}:\n`/usr/bin/whoami`>>>>>>>>>>>>>>>> "'
Then run $source ~/.aliases

Check the date and time entered by user in UNIX

I have a Shell script which uses the date and time parameters entered by the user.
Date as mm/dd/yyyy and Time as HH:MM .
What would be the easiest means to check the user had entered the proper date [ like month should be less than 12.... for time MM should be less than 60...
Do we have any built in functions in UNIX for checking the timestamp?
You could use the unix date tool to parse and verify it for you, and test the return code e.g.
A valid date, return code of 0:
joel#bohr:~$ date -d "12/12/2000 13:00"
Tue Dec 12 13:00:00 GMT 2000
joel#bohr:~$ echo $?
0
An invalid date, return code 1:
joel#bohr:~$ date -d "13/12/2000 13:00"
date: invalid date `13/12/2000 13:00'
joel#bohr:~$ echo $?
1
You can vary the input format accepted by date by using the +FORMAT option (man date)
Putting it all together as a little script:
usrdate=$1
date -d "$usrdate" > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "Date $usrdate was valid"
else
echo "Date $usrdate was invalid"
fi
You could use grep to check that the input conforms to the correct format:
if ! echo "$INPUT" | grep -q 'PATTERN'; then
# handle input error
fi
where PATTERN is a regular expressino that matches all valid inputs and only valid inputs. I leave constructing that pattern to you ;-).
(Late answer)
Something that you can use:
...
DATETIME=$1
#validate datetime..
tmp=`date -d "$DATETIME" 2>&1` ; #return is: "date: invalid date `something'"
if [ "${tmp:6:7}" == "invalid" ]; then
echo "Invalid datetime: $DATETIME" ;
else
... valid datetime, do something with it ...
fi

Resources