Ad Widget

Collapse

Windows Scheduled Task Monitor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ismael Medina
    Junior Member
    • Mar 2007
    • 15

    #1

    Windows Scheduled Task Monitor

    Hi.

    I'm working with Zabbix and has been writting a lot of User Parameters, here is a full source code to monitoring Windows Schedule Task.

    I hope it could help.

    Thanks.

    #include <windows.h>
    #include <initguid.h>
    #include <ole2.h>
    #include <mstask.h>
    #include <msterr.h>
    #include <wchar.h>

    int main(int argc, char *argv[])
    {
    if (argc != 2)
    {
    wprintf(L"SCHEDTASKSTAT [TaskName]\n");
    wprintf(L"\t[TaskName]: Nombre de la tarea que se va a consultar\n");
    return 0;
    }

    HRESULT hr = S_OK;

    ///////////////////////////////////////////////////////////////////
    // Call CoInitialize to initialize the COM library and then
    // call CoCreateInstance to get the Task Scheduler object.
    ///////////////////////////////////////////////////////////////////
    ITaskScheduler *pITS;
    hr = CoInitialize(NULL);
    if (SUCCEEDED(hr))
    {
    hr = CoCreateInstance(CLSID_CTaskScheduler,
    NULL,
    CLSCTX_INPROC_SERVER,
    IID_ITaskScheduler,
    (void **) &pITS);
    if (FAILED(hr))
    {
    CoUninitialize();
    return 1;
    }
    }
    else
    {
    return 1;
    }


    ///////////////////////////////////////////////////////////////////
    // Call ITaskScheduler::Activate to get the Task object.
    ///////////////////////////////////////////////////////////////////
    ITask *pITask;
    int lenTaskName;
    lenTaskName = mbstowcs(NULL, argv[1], 0) + 1;
    wchar_t *wcTaskName = (wchar_t *)malloc(lenTaskName * sizeof (wchar_t));
    mbstowcs(wcTaskName, argv[1], lenTaskName);

    hr = pITS->Activate(wcTaskName,
    IID_ITask,
    (IUnknown**) &pITask);

    // Release ITaskScheduler interface.
    pITS->Release();

    if (FAILED(hr))
    {
    wprintf(L"Failed calling ITaskScheduler::Activate: ");
    wprintf(L"error = 0x%x\n",hr);
    CoUninitialize();
    return 1;
    }


    ///////////////////////////////////////////////////////////////////
    // Call ITask::GetStatus. Note that this method is
    // inherited from IScheduledWorkItem.
    ///////////////////////////////////////////////////////////////////
    HRESULT phrStatus;

    hr = pITask->GetStatus(&phrStatus);

    // Release the ITask interface.

    if (FAILED(hr))
    {
    wprintf(L"Failed calling ITask::GetStatus: ");
    wprintf(L"error = 0x%x\n",hr);
    CoUninitialize();
    return 1;
    }

    SYSTEMTIME pstLastRun;

    hr = pITask->GetMostRecentRunTime(&pstLastRun);

    if (FAILED(hr))
    {
    wprintf(L"Failed calling ITask::GetMostRecentRunTime: ");
    wprintf(L"error = 0x%x\n",hr);
    CoUninitialize();
    return 1;
    }

    pITask->Release();

    wprintf(L"Estatus: ");

    switch(phrStatus)
    {
    case SCHED_S_TASK_READY:
    wprintf(L"READY");
    break;
    case SCHED_S_TASK_RUNNING:
    wprintf(L"RUNNING");
    break;
    case SCHED_S_TASK_DISABLED:
    wprintf(L"DISABLED");
    break;
    case SCHED_S_TASK_HAS_NOT_RUN:
    wprintf(L"HAS_NOT_RUN");
    break;
    case SCHED_S_TASK_NOT_SCHEDULED:
    wprintf(L"NOT_SCHEDULED");
    break;
    case SCHED_S_TASK_NO_MORE_RUNS:
    wprintf(L"NO_MORE_RUNS");
    break;
    case SCHED_S_TASK_NO_VALID_TRIGGERS:
    wprintf(L"NO_VALID_TRIGGERS");
    break;
    default:
    wprintf(L"ERROR!\n");
    return 1;
    }

    wprintf(L",\t Ultima Ejecucion: ");
    wprintf(L"%u/%u/%u %u:%02u:%02u\n", pstLastRun.wDay,
    pstLastRun.wMonth,
    pstLastRun.wYear,
    pstLastRun.wHour,
    pstLastRun.wMinute,
    pstLastRun.wSecond );
    CoUninitialize();
    return 0;
    }
  • dminstrel
    Member
    • Apr 2005
    • 72

    #2
    Thank you very much for this, it's exactly what I was looking for. Can you share a bit more information on your monitoring strategy? Did you create triggers that parse the time of last execution or the returned status? An example trigger definition would be great.

    Best regards,

    Comment

    • Hotzenwalder
      Junior Member
      • Apr 2008
      • 11

      #3
      Could you post a howto on the implementation of this?

      Comment

      Working...