163 lines
5.2 KiB
Python
163 lines
5.2 KiB
Python
import redfish
|
|
import sqlite3
|
|
import bcrypt
|
|
from flask import Flask, jsonify, request
|
|
from flask_cors import CORS
|
|
from database import initialize_db, add_server, get_servers, add_action, get_actions
|
|
|
|
|
|
app = Flask(__name__, static_folder='../frontend', static_url_path='/')
|
|
CORS(app) # Enable CORS
|
|
|
|
initialize_db()
|
|
|
|
username = "admin"
|
|
password = "Pbg68dqUJ"
|
|
|
|
@app.route('/api/servers', methods=['GET'])
|
|
def list_servers():
|
|
servers = get_servers()
|
|
return jsonify(servers)
|
|
|
|
@app.route('/api/actions', methods=['GET'])
|
|
def list_actions():
|
|
actions = get_actions()
|
|
return jsonify(actions)
|
|
|
|
@app.route('/api/add-server', methods=['POST'])
|
|
def add_server_route():
|
|
data = request.json
|
|
ip = data.get('ip')
|
|
name = data.get('name')
|
|
|
|
if not ip or not name:
|
|
return jsonify({"error": "Missing required parameters"}), 400
|
|
|
|
try:
|
|
add_server(ip, name)
|
|
return jsonify({"message": "Server added successfully!"}), 201
|
|
except Exception as e:
|
|
return jsonify({"error": str(e)}), 500
|
|
|
|
@app.route('/api/add-endpoint', methods=['POST'])
|
|
def add_endpoint_route():
|
|
data = request.json
|
|
name = data.get('name')
|
|
endpoint = data.get('endpoint')
|
|
|
|
if not name or not endpoint:
|
|
return jsonify({"error": "Missing required parameters"}), 400
|
|
|
|
try:
|
|
add_action(name, endpoint)
|
|
return jsonify({"message": "Endpoint added successfully!"}), 201
|
|
except Exception as e:
|
|
return jsonify({"error": str(e)}), 500
|
|
|
|
@app.route('/api/server-info', methods=['GET'])
|
|
def server_info():
|
|
ip = request.args.get('ip')
|
|
#username = "admin"
|
|
#password = get_admin_password()
|
|
|
|
if not ip or not username or not password:
|
|
return jsonify({"error": "Missing required parameters"}), 400
|
|
|
|
try:
|
|
# Connect to the iLO
|
|
rest_obj = redfish.LegacyRestClient(base_url=ip, username=username, password=password)
|
|
rest_obj.login()
|
|
response = rest_obj.get('/rest/v1/managers/1')
|
|
rest_obj.logout()
|
|
|
|
# If the response contains data, return it
|
|
if response.status == 200:
|
|
return jsonify(response.dict)
|
|
|
|
else:
|
|
# Return more detailed error information
|
|
return jsonify({
|
|
"error": "Error retrieving data from iLO",
|
|
"status": response.status,
|
|
"message": response.dict # Add this line to capture more details
|
|
}), response.status
|
|
|
|
except Exception as e:
|
|
return jsonify({"error": str(e)}), 500
|
|
|
|
# Get SNMP service information
|
|
@app.route('/api/snmp-info', methods=['GET'])
|
|
def snmp_info():
|
|
ip = request.args.get('ip')
|
|
#username = "admin"
|
|
#password = get_admin_password()
|
|
|
|
if not ip or not username or not password:
|
|
return jsonify({"error": "Missing required parameters"}), 400
|
|
|
|
try:
|
|
# Connect to the iLO
|
|
rest_obj = redfish.LegacyRestClient(base_url=ip, username=username, password=password)
|
|
rest_obj.login()
|
|
# response = rest_obj.get('/redfish/v1/Managers/1/SnmpService/')
|
|
response = rest_obj.get('/redfish/v1/Managers/1/NetworkService/')
|
|
rest_obj.logout()
|
|
|
|
# If the response contains data, return it
|
|
if response.status == 200:
|
|
return jsonify(response.dict)
|
|
|
|
else:
|
|
# Return more detailed error information
|
|
return jsonify({
|
|
"error": "Error retrieving data from iLO",
|
|
"status": response.status,
|
|
"message": response.dict # Add this line to capture more details
|
|
}), response.status
|
|
|
|
except Exception as e:
|
|
return jsonify({"error": str(e)}), 500
|
|
|
|
# Send SNMP test message
|
|
@app.route('/api/send-test-mail', methods=['GET'])
|
|
def snmp_test():
|
|
ip = request.args.get('ip')
|
|
#username = "admin"
|
|
#password = get_admin_password()
|
|
|
|
if not ip or not username or not password:
|
|
return jsonify({"error": "Missing required parameters"}), 400
|
|
|
|
try:
|
|
# Connect to the iLO
|
|
rest_obj = redfish.LegacyRestClient(base_url=ip, username=username, password=password)
|
|
rest_obj.login()
|
|
#response = rest_obj.post('/redfish/v1/Managers/1/SnmpService', body={"Action": "SendSNMPTestAlert"})
|
|
#response = rest_obj.post('/redfish/v1/Managers/1/SnmpService/Actions/SnmpService.SendSNMPTestAlert/', body={})
|
|
#response = rest_obj.post('/redfish/v1/Managers/1/NetworkService/Actions/Oem/Hp/HpiLOManagerNetworkService', body={"Action": "SendTestAlertMail"})
|
|
response = rest_obj.get('/rest/v1/Managers/1/UpdateService')
|
|
rest_obj.logout()
|
|
|
|
# If the response contains data, return it
|
|
if response.status == 200:
|
|
return jsonify(response.dict)
|
|
|
|
else:
|
|
# Return more detailed error information
|
|
return jsonify({
|
|
"error": "Error retrieving data from iLO",
|
|
"status": response.status,
|
|
"message": response.dict # Add this line to capture more details
|
|
}), response.status
|
|
|
|
except Exception as e:
|
|
return jsonify({"error": str(e)}), 500
|
|
|
|
|
|
@app.route('/')
|
|
def serve_frontend():
|
|
return send_from_directory(app.static_folder, 'index.html')
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True)
|