meta-ampere: support Redfish event log

Enable system event log for Redfish. It includes the setting to:
 - Enable sel-logger module which logs the sensor event into the
systemd log.
 - Enable the rsyslog (intel module) to sync the sensor events from
systemd to /var/log/redfish. The file will be used by Redfish.
 - Configure the backend and redfish to get the system log from the
/var/log/redfish instead of system d-bus
 - Enable Redfish journal log

Tested: Verify event logs appear in:
1. Redfish: /redfish/v1/Systems/system/LogServices/EventLog/Entries
2. Redfish: /redfish/v1/Managers/bmc/LogServices/Journal/Entries
3. WebUI: /server-health/sys-log at Event type.

Signed-off-by: Thang Q. Nguyen <thang@os.amperecomputing.com>
Change-Id: I3753e2932ec1dc83cdbe0708173531930df65254
diff --git a/meta-ampere/meta-common/recipes-ac01/packagegroups/packagegroup-ampere-apps.bb b/meta-ampere/meta-common/recipes-ac01/packagegroups/packagegroup-ampere-apps.bb
index 0831988..d29b870 100644
--- a/meta-ampere/meta-common/recipes-ac01/packagegroups/packagegroup-ampere-apps.bb
+++ b/meta-ampere/meta-common/recipes-ac01/packagegroups/packagegroup-ampere-apps.bb
@@ -23,6 +23,9 @@
 RDEPENDS_${PN}-chassis = " \
         obmc-op-control-power \
         ampere-hostctrl \
+        phosphor-hostlogger \
+        phosphor-sel-logger \
+        phosphor-logging \
         "
 
 SUMMARY_${PN}-system = "Ampere System"
diff --git a/meta-ampere/meta-common/recipes-extended/rsyslog/rsyslog/rotate-event-logs.service b/meta-ampere/meta-common/recipes-extended/rsyslog/rsyslog/rotate-event-logs.service
new file mode 100644
index 0000000..8f3a2bc
--- /dev/null
+++ b/meta-ampere/meta-common/recipes-extended/rsyslog/rsyslog/rotate-event-logs.service
@@ -0,0 +1,9 @@
+[Unit]
+Description=Rotate the event logs
+
+[Service]
+Type=simple
+ExecStart=/usr/bin/rotate-event-logs.sh
+
+[Install]
+WantedBy=multi-user.target
diff --git a/meta-ampere/meta-common/recipes-extended/rsyslog/rsyslog/rotate-event-logs.sh b/meta-ampere/meta-common/recipes-extended/rsyslog/rsyslog/rotate-event-logs.sh
new file mode 100644
index 0000000..5a8c5cc
--- /dev/null
+++ b/meta-ampere/meta-common/recipes-extended/rsyslog/rsyslog/rotate-event-logs.sh
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+while true; do
+    sleep 60
+    /usr/sbin/logrotate /etc/logrotate.d/logrotate.rsyslog
+    ec=$?
+    if [ $ec -ne 0 ] ; then
+        echo "logrotate failed ($ec)"
+    fi
+done
diff --git a/meta-ampere/meta-common/recipes-extended/rsyslog/rsyslog/rsyslog-override.conf b/meta-ampere/meta-common/recipes-extended/rsyslog/rsyslog/rsyslog-override.conf
new file mode 100644
index 0000000..14bcc07
--- /dev/null
+++ b/meta-ampere/meta-common/recipes-extended/rsyslog/rsyslog/rsyslog-override.conf
@@ -0,0 +1,2 @@
+[Service]
+ExecReload=/bin/kill -HUP $MAINPID
diff --git a/meta-ampere/meta-common/recipes-extended/rsyslog/rsyslog/rsyslog.conf b/meta-ampere/meta-common/recipes-extended/rsyslog/rsyslog/rsyslog.conf
new file mode 100644
index 0000000..656f1d3
--- /dev/null
+++ b/meta-ampere/meta-common/recipes-extended/rsyslog/rsyslog/rsyslog.conf
@@ -0,0 +1,53 @@
+# if you experience problems, check
+# http://www.rsyslog.com/troubleshoot for assistance
+
+# rsyslog v3: load input modules
+# If you do not load inputs, nothing happens!
+# You may need to set the module load path if modules are not found.
+#
+# Ported from debian's sysklogd.conf
+
+# Journal-style logging
+# Limit to no more than 2000 entries in one minute and enable the
+# journal workaround to avoid duplicate entries
+module(load="imjournal" StateFile="/var/log/state"
+                        RateLimit.Interval="60"
+                        RateLimit.Burst="2000")
+
+# Template for Redfish messages
+# "<timestamp> <MessageId>,<MessageArgs>"
+template(name="RedfishTemplate" type="list") {
+    property(name="timereported" dateFormat="rfc3339")
+    constant(value=" ")
+    property(name="$!REDFISH_MESSAGE_ID")
+    constant(value=",")
+    property(name="$!REDFISH_MESSAGE_ARGS")
+    constant(value="\n")
+}
+
+# Template for Application Crashes
+# "<timestamp> <MessageId>,<MessageArgs>"
+template(name="CrashTemplate" type="list") {
+    property(name="timereported" dateFormat="rfc3339")
+    constant(value=" ")
+    constant(value="OpenBMC.0.1.ServiceFailure")
+    constant(value=",")
+    property(name="$!UNIT")
+    constant(value="\n")
+}
+
+
+# If the journal entry has a Redfish MessageId, save as a Redfish event
+if ($!REDFISH_MESSAGE_ID != "") then {
+   action(type="omfile" file="/var/log/redfish" template="RedfishTemplate")
+}
+
+# If the journal entry has a Exit Code, save as a Redfish event
+if ($!EXIT_STATUS != "" and $!EXIT_STATUS != "0") then {
+   action(type="omfile" file="/var/log/redfish" template="CrashTemplate")
+}
+
+#
+# Include all config files in /etc/rsyslog.d/
+#
+$IncludeConfig /etc/rsyslog.d/*.conf
diff --git a/meta-ampere/meta-common/recipes-extended/rsyslog/rsyslog/rsyslog.logrotate b/meta-ampere/meta-common/recipes-extended/rsyslog/rsyslog/rsyslog.logrotate
new file mode 100644
index 0000000..a6ba28d
--- /dev/null
+++ b/meta-ampere/meta-common/recipes-extended/rsyslog/rsyslog/rsyslog.logrotate
@@ -0,0 +1,22 @@
+# /etc/logrotate.d/rsyslog - Ported from Debian
+
+# Keep up to four 64k files for ipmi_sel (256k total)
+/var/log/ipmi_sel
+{
+    rotate 3
+    size 64k
+    missingok
+    postrotate
+        systemctl reload rsyslog 2> /dev/null || true
+    endscript
+}
+# Keep up to four 64k files for redfish (256k total)
+/var/log/redfish
+{
+    rotate 3
+    size 64k
+    missingok
+    postrotate
+        systemctl reload rsyslog 2> /dev/null || true
+    endscript
+}
diff --git a/meta-ampere/meta-common/recipes-extended/rsyslog/rsyslog_%.bbappend b/meta-ampere/meta-common/recipes-extended/rsyslog/rsyslog_%.bbappend
new file mode 100644
index 0000000..f667fb6
--- /dev/null
+++ b/meta-ampere/meta-common/recipes-extended/rsyslog/rsyslog_%.bbappend
@@ -0,0 +1,24 @@
+FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"
+
+SRC_URI += "file://rsyslog.conf \
+            file://rsyslog.logrotate \
+            file://rotate-event-logs.service \
+            file://rotate-event-logs.sh \
+            file://rsyslog-override.conf \
+           "
+
+FILES_${PN} += "${systemd_system_unitdir}/rsyslog.service.d/rsyslog-override.conf"
+
+PACKAGECONFIG_append = " imjournal"
+
+do_install_append() {
+        install -m 0644 ${WORKDIR}/rotate-event-logs.service ${D}${systemd_system_unitdir}
+        install -d ${D}${systemd_system_unitdir}/rsyslog.service.d
+        install -m 0644 ${WORKDIR}/rsyslog-override.conf \
+                        ${D}${systemd_system_unitdir}/rsyslog.service.d/rsyslog-override.conf
+        install -d ${D}${bindir}
+        install -m 0755 ${WORKDIR}/rotate-event-logs.sh ${D}/${bindir}/rotate-event-logs.sh
+        rm ${D}${sysconfdir}/rsyslog.d/imjournal.conf
+}
+
+SYSTEMD_SERVICE_${PN} += " rotate-event-logs.service"
diff --git a/meta-ampere/meta-common/recipes-phosphor/interfaces/bmcweb_%.bbappend b/meta-ampere/meta-common/recipes-phosphor/interfaces/bmcweb_%.bbappend
new file mode 100644
index 0000000..15fb5e6
--- /dev/null
+++ b/meta-ampere/meta-common/recipes-phosphor/interfaces/bmcweb_%.bbappend
@@ -0,0 +1,7 @@
+FILESEXTRAPATHS_append := "${THISDIR}/${PN}:"
+
+EXTRA_OEMESON_append = " \
+     -Dinsecure-tftp-update=enabled \
+     -Dbmcweb-logging=enabled \
+     -Dredfish-bmc-journal=enabled \
+     "
diff --git a/meta-ampere/meta-common/recipes-phosphor/sel-logger/phosphor-sel-logger_%.bbappend b/meta-ampere/meta-common/recipes-phosphor/sel-logger/phosphor-sel-logger_%.bbappend
new file mode 100644
index 0000000..4be8ddb
--- /dev/null
+++ b/meta-ampere/meta-common/recipes-phosphor/sel-logger/phosphor-sel-logger_%.bbappend
@@ -0,0 +1,2 @@
+# Enable threshold monitoring
+EXTRA_OECMAKE += "-DSEL_LOGGER_MONITOR_THRESHOLD_EVENTS=ON"