Index: /branches/amp_3_6_0/src/webui/webui/htdocs/new/src/hive/composer.py
===================================================================
--- /branches/amp_3_6_0/src/webui/webui/htdocs/new/src/hive/composer.py	(revision 2344)
+++ /branches/amp_3_6_0/src/webui/webui/htdocs/new/src/hive/composer.py	(working copy)
@@ -8,12 +8,17 @@
 from django.http import HttpResponse
 from djproject.an_settings import extension_path, CMDATA
 from elasticsearch import Elasticsearch
+from elasticsearch.exceptions import ElasticsearchException
 from hive.utils import andebug, upload_receive
 import lib.toml as toml
 import lib.yaml as yaml
 import codecs
+import subprocess
+import os
+import ConfigParser
 from django.views.decorators.csrf import csrf_exempt
 from django.views.decorators.gzip import gzip_page
+from cm.lib.libbasic_operation import oper_log
 # import re
 # import requests
 # try:
@@ -22,10 +27,13 @@
 #     from urllib.parse import urlparse
 from revproxy.views import ProxyView
 
+ELASTIC_SEARCH_CONFIG_FILE = '/etc/elasticsearch/elasticsearch.yml'
+
 
 class KibanaProxyView(ProxyView):
     upstream = 'http://localhost:5601'
 
+
 # def get_headers(environ):
 #     """
 #     Retrieve the HTTP headers from a WSGI environment dictionary.  See
@@ -140,7 +148,6 @@
 @csrf_exempt
 @gzip_page
 def composer_proxy(request, path):
-
     url = "/v1/" + path
     timestamp = str(int(time.time()))
     m = hashlib.md5()
@@ -191,7 +198,6 @@
 
 
 def reporting_downloading_handler(request, app, filename):
-
     os.chdir(os.path.join(CMDATA['COMPOSER_REPORT_PATH'], app))
     for dirpath, dirnames, filenames in os.walk("./"):
         for name in filenames:
@@ -211,7 +217,6 @@
 
 
 def reporting_logo_handler(request):
-
     os.chdir('/var/opt/composer/reporting/charts/logo')
     if request.method == 'GET':
         if os.path.exists("logo.png"):
@@ -309,6 +314,7 @@
         lang = config['app']['language']
     except Exception:
         config = toml.load("/ca/webui/conf/composer_ui.toml")
+
         lang = config['app']['language']
 
     if request.method == 'GET':
@@ -330,7 +336,6 @@
 
 
 def composer_config(request, app):
-
     if app == 'acquisition':
         return acquisition_config(request)
     elif app == 'elastic':
@@ -339,57 +344,192 @@
         return kibana_config(request)
     elif app == 'composer_ui':
         return composer_ui_config(request)
+    elif app == 'is_external_drive_available':
+        return is_external_drive_available()
+    elif app == 'switch_log_location':
+        return change_log_location(request)
+
+
+def is_external_drive_available():
+    try:
+        # Run the df -h command and capture the output
+        result = subprocess.check_output(['df', '-h'], universal_newlines=True)
+
+        # Split the output into lines
+        lines = result.splitlines()
+
+        # Extract the 'Mounted on' column values
+        mounted_on_values = []
+        for line in lines[1:]:  # Skip the header line
+            parts = line.split()
+            if 'external' in parts[-1]:
+                return HttpResponse(json.dumps({'is_external_drive_available': True}))
+        return HttpResponse(json.dumps({'is_external_drive_available': False}))
+    except subprocess.CalledProcessError as e:
+        oper_log('error', 'system', 'Error running df -h: {}'.format(e.output))
+        return HttpResponse(json.dumps({
+            'error': 500,
+            'message': "Error while checking external drive is available in system",
+            'data': str(e)
+        }), content_type='application/json')
+
+
+def change_log_location(request):
+    create_elastic_search_dir()
+    try:
+        obj = json.loads(request.body)
+    except json.JSONDecodeError:
+        oper_log('error', 'system', 'Invalid JSON:{}'.format(str(obj)))
+        return HttpResponse(json.dumps({
+            'error': 400,
+            'message': "Invalid JSON:{}".format(str(obj))
+        }), content_type='application/json')
+
+    if 'log_location' in obj:
+        log_location = obj['log_location']
+    else:
+        oper_log('error', 'system', 'Log Location not specified')
+        return HttpResponse(json.dumps({
+            'error': 400,
+            'message': "Log Location not specified"
+        }), content_type='application/json')
+
+    if log_location == 'external':
+        log_location = '/external/elasticsearch'
+    elif log_location == 'internal':
+        log_location = '/var/log/elasticsearch'
+    else:
+        oper_log('error', 'system', 'Invalid location specified: {}'.format(log_location))
+        return HttpResponse(json.dumps({
+            'error': 400,
+            'message': "Invalid location specified: {}. Valid values are 'internal' and 'external'".format(log_location)
+        }), content_type='application/json')
+
+    # Read existing configuration
+    with open(ELASTIC_SEARCH_CONFIG_FILE, 'r') as configfile:
+        lines = configfile.readlines()
+
+    updated_lines = []
+    for line in lines:
+        if 'path.logs' in line:
+            updated_lines.append("path.logs: {}\n".format(log_location))
+        else:
+            updated_lines.append(line)
+
+    # write the updated configuration back to the file
+    try:
+        with open(ELASTIC_SEARCH_CONFIG_FILE, 'w') as configfile:
+            configfile.writelines(updated_lines)
+    except IOError as e:
+        oper_log('error', 'system', 'Failed to write to config file')
+        return HttpResponse(json.dumps({
+            'error': 500,
+            'message': "Failed to write to config file",
+            'data': str(e)
+        }), content_type='application/json')
+
+    try:
+        # Restart the Elasticsearch service
+        subprocess.check_call('systemctl restart elasticsearch', shell=True)
+        return HttpResponse(json.dumps({'status': 'success'}))
+    except subprocess.CalledProcessError as e:
+        # Log the error (optional)
+        oper_log('error', 'system', 'Error during elastic search restart: {}'.format(e.output))
+        return HttpResponse(json.dumps({
+            'error': 500,
+            'message': "Failed to restart Elasticsearch service",
+            'data': str(e)
+        }), content_type='application/json')
+    except Exception as e:
+        # Handle unexpected exceptions
+        oper_log('error', 'system', 'Error during elastic search restart: {}'.format(e.output))
+        return HttpResponse(json.dumps({
+            'error': 500,
+            'message': "An unexpected error occurred",
+            'data': str(e)
+        }), content_type='application/json')
 
-    # if request.method == 'GET':
-    #     if app == 'language':
-    #         config = toml.load("/var/opt/composer/ui/conf/composer_ui.toml")
-    #         language = config['app']['language']
-    #         if language == 'zh-CN':
-    #             language = 'zh-cn'
-    #         response = HttpResponse(language)
-    #         response.status_code = 200
-    #     elif app == 'log_origin':
-    #         config = toml.load(
-    #             os.path.join(extension_path, "auditing", "syslogd",
-    #                          "acm_syslogd.conf"))
-    #         # andebug("hive.debug", config)
-    #         for listener in config['inputs']['socket_listener']:
-    #             value = listener['log_origin']
-    #         response = HttpResponse(value)
-    #         response.status_code = 200
-
-    # elif request.method == 'POST':
-    #     obj = json.loads(request.body)
-    #     value = obj['value']
-    #     if app == 'language':
-    #         config = toml.load("/var/opt/composer/ui/conf/composer_ui.toml")
-    #         if value == 'zh-cn':
-    #             value = 'zh-CN'
-    #         config['app']['language'] = value
-    #         # andebug("hive.debug", config)
-    #         with open("/var/opt/composer/ui/conf/composer_ui.toml", 'w') as f:
-    #             toml.dump(config, f)
-    #     elif app == 'log_origin':
-    #         config = toml.load(
-    #             os.path.join(extension_path, "auditing", "syslogd",
-    #                          "acm_syslogd.conf"))
-    #         for listener in config['inputs']['socket_listener']:
-    #             listener['log_origin'] = value
-    #         # andebug("hive.debug", config)
-    #         with open(
-    #                 os.path.join(extension_path, "auditing", "syslogd",
-    #                              "acm_syslogd.conf"), 'w') as f:
-    #             toml.dump(config, f)
-    #         os.system('service acm_syslogd restart')
-    #         time.sleep(3)
-    #     response = HttpResponse(json.dumps({'is_success': True}))
 
-    # return response
+def create_elastic_search_dir():
+    directory = '/external/elasticsearch'
+
+    if not os.path.exists(directory):
+        commands = [
+            "mkdir -p {}".format(directory),
+            "chown -R elasticsearch:elasticsearch {}".format(directory),
+            "chmod 750 {}".format(directory)
+        ]
+        for command in commands:
+            try:
+                oper_log('info', 'system', 'Executing command :{}'.format(command))
+                subprocess.check_call(command, shell=True)
+            except subprocess.CalledProcessError as e:
+                oper_log('info', 'system', 'Executing command :{} failed with error:{}'.format(command, e.output))
+                return HttpResponse(
+                    {'status': 'error',
+                     'message': 'Executing command :{} failed with error:{}'.format(command, e.output)},
+                    status=500)
+            except Exception as e:
+                # Handle unexpected exceptions
+                oper_log('info', 'system', 'Executing command :{} failed with error:{}'.format(command, e.output))
+                return HttpResponse(json.dumps({
+                    'error': 500,
+                    'message': "An unexpected error occurred",
+                    'data': str(e)
+                }), content_type='application/json')
+
+
+# if request.method == 'GET':
+#     if app == 'language':
+#         config = toml.load("/var/opt/composer/ui/conf/composer_ui.toml")
+#         language = config['app']['language']
+#         if language == 'zh-CN':
+#             language = 'zh-cn'
+#         response = HttpResponse(language)
+#         response.status_code = 200
+#     elif app == 'log_origin':
+#         config = toml.load(
+#             os.path.join(extension_path, "auditing", "syslogd",
+#                          "acm_syslogd.conf"))
+#         # andebug("hive.debug", config)
+#         for listener in config['inputs']['socket_listener']:
+#             value = listener['log_origin']
+#         response = HttpResponse(value)
+#         response.status_code = 200
+
+# elif request.method == 'POST':
+#     obj = json.loads(request.body)
+#     value = obj['value']
+#     if app == 'language':
+#         config = toml.load("/var/opt/composer/ui/conf/composer_ui.toml")
+#         if value == 'zh-cn':
+#             value = 'zh-CN'
+#         config['app']['language'] = value
+#         # andebug("hive.debug", config)
+#         with open("/var/opt/composer/ui/conf/composer_ui.toml", 'w') as f:
+#             toml.dump(config, f)
+#     elif app == 'log_origin':
+#         config = toml.load(
+#             os.path.join(extension_path, "auditing", "syslogd",
+#                          "acm_syslogd.conf"))
+#         for listener in config['inputs']['socket_listener']:
+#             listener['log_origin'] = value
+#         # andebug("hive.debug", config)
+#         with open(
+#                 os.path.join(extension_path, "auditing", "syslogd",
+#                              "acm_syslogd.conf"), 'w') as f:
+#             toml.dump(config, f)
+#         os.system('service acm_syslogd restart')
+#         time.sleep(3)
+#     response = HttpResponse(json.dumps({'is_success': True}))
+
+# return response
 
 
 def procIsRunning(process_name):
     try:
-        process = len(os.popen('ps aux | grep "' + process_name + '" | grep -v grep | grep -v tail | grep -v keepH5ssAlive').readlines())
+        process = len(os.popen(
+            'ps aux | grep "' + process_name + '" | grep -v grep | grep -v tail | grep -v keepH5ssAlive').readlines())
         if process >= 1:
             return True
         else:
@@ -427,3 +567,4 @@
         return elastic_status(request)
     elif app == 'kibana':
         return kibana_status(request)
+
