dreport: Enable "elog" data collection type

Enabled this support dump collection during application
specific error log commit.

Change-Id: I297a2d28c7f302b5126fb7e0792be8b68d6928ac
Signed-off-by: Jayanth Othayoth <ojayanth@in.ibm.com>
diff --git a/tools/dreport b/tools/dreport
index e8c32a5..d37131c 100755
--- a/tools/dreport
+++ b/tools/dreport
@@ -19,12 +19,13 @@
                               Identifiers include numeric characters.
                               Default dump identifier is 0
         -t, —-type <type>     Data collection type. Valid types are
-                              "user", "core".
+                              "user", "core", "elog".
                               Default type is "user" initiated.
         -p, —-path <path>     Optional contents to be included in the archive.
                               Valid paths are absolute file path or d-bus path
                               based on type parameter.
                                  -Absolute file path for "core" type.
+                                 -elog d-bus object for "elog" type.
         -s, --size <size>     Maximum allowed size(in KB) of the archive.
                               Report will be truncated in case size exceeds
                               this limit. Default size is unlimited.
@@ -41,6 +42,7 @@
 declare -r SUMMARY_DUMP="summary"
 declare -r TYPE_USER="user"
 declare -r TYPE_CORE="core"
+declare -r TYPE_ELOG="elog"
 declare -r SUMMARY_LOG="summary.log"
 declare -r DREPORT_LOG="dreport.log"
 declare -r TMP_DIR="/tmp"
@@ -67,25 +69,35 @@
 declare -x cur_dump_size=0
 declare -a command_list=("")
 declare -x pid="0"
+declare -x elog_id=""
+
+# @brief Initialize general command array
+#        Set of commands, which provide general debug information
+function runlevel_general()
+{
+command_list=(
+    get_fw_level
+    bmc_state
+    host_state
+    chassis_state
+    get_host_info
+    get_uname
+    get_uptime
+    get_disk_usage
+    get_host_info
+    get_cpuinfo
+    get_meminfo
+    get_top
+    )
+}
 
 # @brief Initialize user type command array
 function runlevel_user()
 {
 command_list=(
-    get_fw_level
-    get_uname
-    get_uptime
-    get_disk_usage
-    bmc_state
-    host_state
-    chassis_state
-    get_host_info
-    get_obmc_console
-    get_cpuinfo
-    get_meminfo
-    get_top
-    get_elog_all
     get_journal
+    get_elog_all
+    get_obmc_console
     get_failed_services
     )
 }
@@ -96,20 +108,18 @@
 command_list=(
     move_optional_file
     get_proc_journal
-    get_fw_level
-    get_uname
-    get_uptime
-    get_disk_usage
-    bmc_state
-    host_state
-    chassis_state
-    get_host_info
     get_failed_services
     get_obmc_console
-    get_cpuinfo
-    get_meminfo
-    get_top
-)
+    )
+}
+
+# @brief Initialize elog type command array
+function runlevel_elog()
+{
+command_list=(
+    get_proc_journal
+    get_elog
+    )
 }
 
 function get_fw_level()
@@ -244,6 +254,7 @@
     run_command "$command" "$copy_loc" "$desc"
 }
 
+# @brief get all available elogs in BMC.
 function get_elog_all()
 {
     desc="elog"
@@ -269,6 +280,20 @@
     run_command "$command" "$copy_loc" "$desc"
 }
 
+# @brief get elog specific to global elog_id
+function get_elog()
+{
+    desc="elog id:$elog_id"
+
+    command="busctl --verbose --no-pager \
+                     call xyz.openbmc_project.Logging \
+                     $optional_path \
+                     org.freedesktop.DBus.Properties GetAll s \
+                     xyz.openbmc_project.Logging.Entry"
+    copy_loc="elog_$elog_id.log"
+    run_command "$command" "$copy_loc" "$desc"
+}
+
 function move_optional_file()
 {
     desc="Move Optional file"
@@ -311,6 +336,9 @@
 # @return 0 on success, error code otherwise
 function collect_data()
 {
+    runlevel_general
+    capture_data "${command_list[@]}"
+
     case $dump_type in
         $TYPE_USER)
             runlevel_user
@@ -321,6 +349,13 @@
             runlevel_core
             capture_data "${command_list[@]}"
             ;;
+        $TYPE_ELOG)
+            elog_id=$(basename "$optional_path")
+            set_elog_pid
+            runlevel_elog
+            capture_data "${command_list[@]}"
+            ;;
+
         $SUMMARY_DUMP)
             #No data collection is required.
             ;;
@@ -345,6 +380,23 @@
     pid=$(echo $file | awk -F . '{ print $5}')
 }
 
+# @brief set elog pid by reading _PID information from the elog d-bus object.
+#        _PID information is stored  elog Additional data field
+#        Data format  "_PID=<pid>"
+function set_elog_pid()
+{
+    additional_data=$(busctl get-property xyz.openbmc_project.Logging \
+                             $optional_path \
+                             xyz.openbmc_project.Logging.Entry \
+                             AdditionalData)
+
+    #read _PID data.
+    if [ ! -z "$additional_data" ]; then
+        pid=$(echo $additional_data | \
+                   awk -F _PID= '{ print ($2+0)}')
+    fi
+}
+
 # @brief Capture debug data based on the input command array.
 #        and stores in to global temporary name specific location.
 # @param $1 Source array
@@ -444,7 +496,9 @@
     summary_log="$name_dir/$SUMMARY_LOG"
 
     #Type
-    if [[ !($dump_type = $TYPE_USER || $dump_type = $TYPE_CORE) ]]; then
+    if [[ !($dump_type = $TYPE_USER || \
+            $dump_type = $TYPE_CORE || \
+            $dump_type = $TYPE_ELOG) ]]; then
        log_error "Invalid -type, Only summary log is available"
        dump_type=$SUMMARY_DUMP
     fi