ehrnst / System-Center-Operations-Manager-API

Microsoft System Center Operations Manager (SCOM) Web API
http://adatum.no/operationsmanager/web-api-for-scom
MIT License
41 stars 6 forks source link

Function to end maintenance mode #9

Open ehrnst opened 7 years ago

ehrnst commented 7 years ago

A way to stop/end maintenance mode is needed.

neutmute commented 7 years ago

Hey there - I'm a dev and one of my ops guys has installed your project and wants me to use it to do exactly this - start and end maintenance mode. I'm not a SCOM expert, so to confirm what I understand

  1. This is an unofficial project that exposes SCOM API in a nice RESTful interface?
  2. There is no nuget api for clients - but we could probably make one using swagger like this
  3. Maintenance mode can be started via this API, but you need someone to jump in and wireup the Maintenance mode end

Naively, it seems like the last point shouldn't be too hard? Happy to jump in and do a PR soon. Any pointers / landmines to be aware of?

For context - what we want to do is plug SCOM into our Octopus deploys, starting and ending maintenance mode for our servers during the deployment

ehrnst commented 7 years ago

Hi, thanks for your interest. My respond will be as an IT-pro as i'm not a dev :)

1: In general, yes. The API loads SCOM sdk through its DLLs and exposes some of the functionality as a WebAPI. I belive it's restful, but maybe not everything. As unOfficial as its gets. 2: The API have Swagger enabled for documentation and testing 3: You are correct. The current version does not have the ability to end maintenance atm. I have a local version where that is started. It will need some more work. Especially for the schedule type. Below is the code to update/end maintenance for a object.

Personally, I dont know of any pit falls other than the code is written by me, as my first C# project. Meaning it might be full of non best practice stuff.

If you have a blog or anything, It would be cool to see a little article on your project, or a guest blog if you like :)

` ///

/// Updates or ends existing maintenance mode for object. /// /// Json string with object id required /// If true, maintenance will end /// /// { /// "id": "Guid", /// "EndTime": "2017-07-07T19:00:00.000Z" /// } /// /// Successfully updated maintenance mode for the object /// Bad request. Check json input /// Conflict: object not in maintenance mode

    [HttpPut]
    [ResponseType(typeof(IEnumerable<SCOMObjectMaintenanceModel>))]
    [Route("API/ObjectMaintenance")]
    public IHttpActionResult UpdateObjectMaintenance(SCOMObjectMaintenanceModel Data, bool EndNow)
    {
        //Validate input
        if (string.IsNullOrEmpty(Data.id))
        {
            HttpResponseMessage res = new HttpResponseMessage(HttpStatusCode.BadRequest);
            res.Content = new StringContent("Missing a required parameter?");
            throw new HttpResponseException(res);
        }

        //create a Guid from the json input
        var ObjectId = new Guid(Data.id);
        //get the monitoring object by Guid
        var monObject = mg.EntityObjects.GetObject<MonitoringObject>(ObjectId, ObjectQueryOptions.Default);

        List<SCOMMonitoringObjectModel> MonitoringObjects = new List<SCOMMonitoringObjectModel>();

        List<SCOMObjectMaintenanceModel> MaintenanceObjects = new List<SCOMObjectMaintenanceModel>();

        //If object not in maintenance - throw conflict error
        if (!monObject.InMaintenanceMode)
        {
            {
                HttpResponseMessage res = new HttpResponseMessage(HttpStatusCode.Conflict);
                res.Content = new StringContent("Specified object not in maintenance mode. Nothing to update...");
                throw new HttpResponseException(res);
            }
        }

        //If object in maintenanance update
        else
        {
            //If endNow parameter validate true. End maintenance mode
            if (EndNow.Equals(true))
            {
                monObject.StopMaintenanceMode(DateTime.UtcNow, TraversalDepth.Recursive);

            }

            //If user specifies an end date
            if (Data.EndTime > DateTime.MinValue)
            {
                MaintenanceWindow MaintenanceWindow = monObject.GetMaintenanceWindow();

                //Compare specified end time with current maintenance end time
                int TimeCompare = DateTime.Compare(Data.EndTime, MaintenanceWindow.ScheduledEndTime);
                //Update end time but use same reason and comment
                monObject.UpdateMaintenanceMode(Data.EndTime, MaintenanceWindow.Reason, MaintenanceWindow.Comments);
            }

        }
        //Return list of computers as Json
        SCOMObjectMaintenanceModel maintenanceObject = new SCOMObjectMaintenanceModel();
        maintenanceObject.displayName = monObject.DisplayName;
        maintenanceObject.id = monObject.Id.ToString();
        maintenanceObject.EndTime = Data.EndTime;
        return Json(maintenanceObject);
    }

`

neutmute commented 7 years ago

it might be full of non best practice stuff

I've spotted a couple of things :) Are you open to some (maybe a lot ;) refactoring in a PR? eg: Namespaces, class names, general structure?

Over the weekend I had a thought that, for my use case I can call the SCOM libraries from our console app directly and don't need a Web API intermediary...Will keep you posted.

ehrnst commented 7 years ago

As long as I can some how understand why and how you made the changes that is fine with me. My priority was to sort out the bugs and some of the much needed functions at this point.

neutmute commented 7 years ago

Just letting you know I'm going to reference the SCOM libs directly from a console app we have - so no need for your API.

I made a nuget package of the SCOM binaries - see #15 - HTH!

ehrnst commented 6 years ago

Dev branch contains function to update or end maintenance mode for monitoring objects. Schedule still missing