Merge remote-tracking branch 'dk/logging'

Change-Id: Ic4d9a5815092316de981258a6c072a59c0bd1dbd
diff --git a/thalerj/openbmctool.py b/thalerj/openbmctool.py
index ef12d81..9fac4f0 100755
--- a/thalerj/openbmctool.py
+++ b/thalerj/openbmctool.py
@@ -2078,7 +2078,81 @@
     #display the information
     return displayFWInvenory(firmwareInfoDict, args)
     
-    
+ 
+def restLogging(host, args, session):
+    """
+         Called by the logging function. Turns REST API logging on/off.
+           
+         @param host: string, the hostname or IP address of the bmc
+         @param args: contains additional arguments used by the logging sub command
+         @param session: the active session to use
+         @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption 
+    """
+
+    url="https://"+host+"/xyz/openbmc_project/logging/rest_api_logs/attr/Enabled"
+    httpHeader = {'Content-Type':'application/json'}
+
+    if(args.rest_logging == 'on'):
+        data = '{"data": 1}'
+    elif(args.rest_logging == 'off'):
+        data = '{"data": 0}'
+    else:
+        return "Invalid logging rest_api command"
+
+    try:
+        res = session.put(url, headers=httpHeader, data=data, verify=False, timeout=30)
+    except(requests.exceptions.Timeout):
+        return(connectionErrHandler(args.json, "Timeout", None))
+    return res.text
+
+
+def remoteLogging(host, args, session):
+    """
+         Called by the logging function. View config information for/disable remote logging (rsyslog).
+           
+         @param host: string, the hostname or IP address of the bmc
+         @param args: contains additional arguments used by the logging sub command
+         @param session: the active session to use
+         @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption 
+    """
+
+    url="https://"+host+"/xyz/openbmc_project/logging/config/remote"
+    httpHeader = {'Content-Type':'application/json'}
+
+    try:
+        if(args.remote_logging == 'view'):
+            res = session.get(url, headers=httpHeader, verify=False, timeout=30)
+        elif(args.remote_logging == 'disable'):
+            res = session.put(url + '/attr/Port', headers=httpHeader, json = {"data": 0}, verify=False, timeout=30)
+            res = session.put(url + '/attr/Address', headers=httpHeader, json = {"data": ""}, verify=False, timeout=30)
+        else:
+            return "Invalid logging remote_logging command"
+    except(requests.exceptions.Timeout):
+        return(connectionErrHandler(args.json, "Timeout", None))
+    return res.text
+
+
+def remoteLoggingConfig(host, args, session):
+    """
+         Called by the logging function. Configures remote logging (rsyslog).
+           
+         @param host: string, the hostname or IP address of the bmc
+         @param args: contains additional arguments used by the logging sub command
+         @param session: the active session to use
+         @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption 
+    """
+
+    url="https://"+host+"/xyz/openbmc_project/logging/config/remote"
+    httpHeader = {'Content-Type':'application/json'}
+
+    try:
+        res = session.put(url + '/attr/Port', headers=httpHeader, json = {"data": args.port}, verify=False, timeout=30)
+        res = session.put(url + '/attr/Address', headers=httpHeader, json = {"data": args.address}, verify=False, timeout=30)
+    except(requests.exceptions.Timeout):
+        return(connectionErrHandler(args.json, "Timeout", None))
+    return res.text
+
+
 def createCommandParser():
     """
          creates the parser for the command line along with help for each command and subcommand
@@ -2251,6 +2325,26 @@
     fwprint.add_argument('-v', '--verbose', action='store_true', help='Verbose output')
     fwprint.set_defaults(func=firmwareList)
     
+    #logging
+    parser_logging = subparsers.add_parser("logging", help="logging controls")
+    logging_sub = parser_logging.add_subparsers(title='subcommands', description='valid subcommands',help="sub-command help", dest='command')
+    
+    #turn rest api logging on/off
+    parser_rest_logging = logging_sub.add_parser("rest_api", help="turn rest api logging on/off")
+    parser_rest_logging.add_argument('rest_logging', choices=['on', 'off'], help='The control option for rest logging: on, off')
+    parser_rest_logging.set_defaults(func=restLogging)
+
+    #remote logging
+    parser_remote_logging = logging_sub.add_parser("remote_logging", help="Remote logging (rsyslog) commands")
+    parser_remote_logging.add_argument('remote_logging', choices=['view', 'disable'], help='Remote logging (rsyslog) commands')
+    parser_remote_logging.set_defaults(func=remoteLogging)
+
+    #configure remote logging
+    parser_remote_logging_config = logging_sub.add_parser("remote_logging_config", help="Configure remote logging (rsyslog)")
+    parser_remote_logging_config.add_argument("-a", "--address", required=True, help="Set IP address of rsyslog server")
+    parser_remote_logging_config.add_argument("-p", "--port", required=True, type=int, help="Set Port of rsyslog server")
+    parser_remote_logging_config.set_defaults(func=remoteLoggingConfig)
+    
     return parser
 
 def main(argv=None):
@@ -2308,7 +2402,8 @@
         else:
             print("usage: openbmctool.py [-h] -H HOST -U USER [-A | -P PW] [-j]\n" +
                       "\t[-t POLICYTABLELOC] [-V]\n" +
-                      "\t{fru,sensors,sel,chassis,collect_service_data,health_check,dump,bmc,mc,gardclear,firmware}\n" +
+                      "\t{fru,sensors,sel,chassis,collect_service_data, \
+                          health_check,dump,bmc,mc,gardclear,firmware,logging}\n" +
                       "\t...\n" +
                       "openbmctool.py: error: the following arguments are required: -H/--host, -U/--user")
             sys.exit()