Scripts/ProtectionJob/Get-CohesityProtectionJobStatus.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
class ProtectionJobStatus {
    [String]$jobName
    [Boolean]$remoteCopy
    [int]$jobId
    $startTime
    $estimatedTime
    [double]$percentCompleted

    ProtectionJobStatus(){}
    ProtectionJobStatus(
        [String]$jobName,
        [Boolean]$remoteCopy,
        [int]$jobId,
        $startTime,
        $estimatedTime,
        [double]$percentCompleted
    ){
        $this.jobName = $jobName
        $this.remoteCopy = $remoteCopy
        $this.jobId = $jobId
        $this.startTime = $startTime
        $this.estimatedTime = $estimatedTime
        $this.percentCompleted  = $percentCompleted
    }
    [string]ToString(){
        return $this | Select-Object -Property *
    }
    [string]GetEstimatedTime() {
        $ts =  [timespan]::fromseconds($this.estimatedTime)
        return ("{0:hh\:mm\:ss\ }" -f $ts)
    }
    [string]GetStartTime() {
        if($this.startTime -eq 0) {
            return "0"
        }
        $ret = ([DateTimeOffset]::FromUnixTimeSeconds($this.startTime)).DateTime.ToLocalTime()
        return $ret.ToString()
    }
}

function Get-CohesityProtectionJobStatus {
    [CmdletBinding()]
    Param(
    )

    Begin {
        if(-not (Test-Path -Path "$HOME/.cohesity"))
        {
          throw "Failed to authenticate. Please connect to the Cohesity Cluster using 'Connect-CohesityCluster'"
        }
        $session = Get-Content -Path $HOME/.cohesity | ConvertFrom-Json

        $server = $session.ClusterUri

        $token = $session.Accesstoken.Accesstoken
    }

    Process {
        $url = $server + '/irisservices/api/v1/public/protectionJobs?isDeleted=false'

        $headers = @{'Authorization'='Bearer '+$token}
        $resp = Invoke-RestApi -Method 'Get' -Uri $url -Headers $headers
        $jobIdAndName = @{}
        $activeJobIds = New-Object System.Collections.ArrayList
        ForEach ($item in $resp) {
             $r = $activeJobIds.Add($item.id)
             $jobIdAndName.Add($item.id,$item.name)
        }

        $jobIdAndRemoteStatus = @{}
        $url = $server + '/irisservices/api/v1/public/protectionRuns'
        $resp = Invoke-RestApi -Method 'Get' -Uri $url -Headers $headers
        foreach ($item in $resp) {
            if($item.backupRun.status -eq "kSuccess") {
                if($false -eq $jobIdAndRemoteStatus.ContainsKey($item.jobId)) {
                    $r = $jobIdAndRemoteStatus.Add($item.jobId, $false)
                }
            } else {
                $remoteStatus = $false
                foreach ($status in $item.copyRun) {
                    if($status.target.type -eq "kRemote") {
                        $remoteStatus = $true
                        break
                    }
                }
                $jobIdAndRemoteStatus[$item.jobId] = $remoteStatus
            }
        }

        $protectionJobStatusList = @()
        $activeTasks = New-Object System.Collections.ArrayList
        $url = $server + '/irisservices/api/v1/backupjobssummary?_includeTenantInfo=true&allUnderHierarchy=true&includeJobsWithoutRun=true&isDeleted=false&numRuns=1000&onlyReturnBasicSummary=true&onlyReturnJobDescription=false'
        $resp = Invoke-RestApi -Method 'Get' -Uri $url -Headers $headers
        ForEach ($item in $resp) {
            if($activeJobIds.Contains($item.backupJobSummary.jobDescription.jobId)) {
                if($item.backupJobSummary.lastProtectionRun.backupRun.base.publicStatus -notin "kSuccess" -AND $null -notlike $item.backupJobSummary.lastProtectionRun.backupRun.activeAttempt.base.progressMonitorTaskPath) {
                    $r = $activeTasks.Add($item.backupJobSummary.lastProtectionRun.backupRun.activeAttempt.base.progressMonitorTaskPath)
                } else {
                    [ProtectionJobStatus]$status = [ProtectionJobStatus]::new(
                        $jobIdAndName[$item.backupJobSummary.jobDescription.jobId],
                        $false,
                        $item.backupJobSummary.jobDescription.jobId,
                        0,
                        0,
                        100
                        )
                    $protectionJobStatusList += $status
                }
            }
        }
        foreach ($item in $activeTasks) {
            $url = $server + '/irisservices/api/v1/progressMonitors?=excludeSubTasks=true&includeFinishedTasks=true&taskPathVec='+$item
            $resp = Invoke-RestApi -Method 'Get' -Uri $url -Headers $headers
            $task = $resp.resultGroupVec[0].taskVec[0].progress
            $jobName = ""
            $jobId = 0
            $startTimeUsecs = 0
            foreach ($jobAttrib in $resp.resultGroupVec[0].taskVec[0].progress.attributeVec) {
                if($jobAttrib.key -eq "job_name") {
                    $jobName = $jobAttrib.value.data.stringValue
                }
                if($jobAttrib.key -eq "job_id") {
                    $jobId = $jobAttrib.value.data.stringValue
                }
                if($jobAttrib.key -eq "start_time_usecs") {
                    $startTimeUsecs = $jobAttrib.value.data.stringValue
                }
            }
            $remoteJobStatus = $false
            foreach($entry in $jobIdAndRemoteStatus.Keys) {
                if($jobId -eq $entry) {
                    $remoteJobStatus = $jobIdAndRemoteStatus[$entry]
                    break
                }
            }
            [ProtectionJobStatus]$status = [ProtectionJobStatus]::new(
                $jobName,
                $remoteJobStatus,
                $jobId,
                $task.startTimeSecs,
                $task.expectedTimeRemainingSecs,
                $task.percentFinished
                )
            $protectionJobStatusList += $status
        }

        $columnWidth = 20
        $protectionJobStatusList | Sort-Object  -Property startTime  -Descending |
        Format-Table @{ Label='ID'; Expression={$_.jobId};},  
        @{ Label='NAME'; Expression={$_.jobName}; Width=$columnWidth; },
        @{ Label='REMOTE COPY'; Expression={$_.remoteCopy}; Width=$columnWidth },
        @{ Label='STARTED AT'; Expression={$_.GetStartTime()}; Width=$columnWidth },
        @{ Label='ESTIMATED TIME'; Expression={$_.GetEstimatedTime()};Width=$columnWidth },
        @{ Label='COMPLETED(%)'; Expression={$_.percentCompleted}; Width=$columnWidth }
    }

    End {
    }
}