0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CPU負荷

Last updated at Posted at 2024-06-18

Shell script

#!/bin/bash

# 目標とするCPU使用率(us+sy)
TARGET_CPU_USAGE=10

# CPU負荷をかけるプロセス数
PROCESS_COUNT=1

# topコマンドでCPU使用率を取得する関数
get_cpu_usage() {
    # topコマンドの出力からusとsyの値を抽出
    cpu_usage=$(top -bn 1 | grep 'Cpu(s)' | sed 's/.*id.* //' | awk '{print $1+$4}')
    echo $cpu_usage
}

# 無限ループでCPU負荷をかける関数
create_cpu_load() {
    while true; do
        yes > /dev/null 2>&1 &
        ((PROCESS_COUNT++))
    done
}

# メインループ
while true; do
    # 現在のCPU使用率を取得
    current_usage=$(get_cpu_usage)

    # 目標のCPU使用率との差分を計算
    diff=$(($TARGET_CPU_USAGE - $current_usage))

    # 目標のCPU使用率より低い場合
    if [ $diff -gt 0 ]; then
        # 新しいプロセスを作成
        create_cpu_load &
    # 目標のCPU使用率より高い場合
    elif [ $diff -lt 0 ]; then
        # プロセスを1つ停止
        pkill -f yes
        ((PROCESS_COUNT--))
    fi

    # 一定時間待つ
    sleep 5
done

バッチ

@echo off

rem CactiサーバーURL
set CACTI_URL=http://<CactiサーバーURL>/
rem Cactiログイン情報
set CACTI_USERNAME=<Cactiユーザー名>
set CACTI_PASSWORD=<Cactiパスワード>
rem 取得したいルータID
set ROUTER_ID=<ルータID>
rem 開始日時 (YYYY-MM-DD HH:MM:SS)
set START_TIME=<開始日時>
rem 終了日時 (YYYY-MM-DD HH:MM:SS)
set END_TIME=<終了日時>

rem Cactiトークン取得
curl -s -X POST -d "action=authenticate&username=%CACTI_USERNAME%&password=%CACTI_PASSWORD%" %CACTI_URL%/api/v1/auth.php | set /p AUTH_TOKEN=

rem トークンを使ってすべてのインターフェースのトラフィックデータ取得
curl -s -H "X-Auth-Token: %AUTH_TOKEN%" %CACTI_URL%/api/v1/host.php?action=get_perfdata&id=%ROUTER_ID%&filter=if_id%3D*&start=%START_TIME%&end=%END_TIME%&output_format=csv | for /F "tokens=*" %%a in (response) do (
    set INTERFACE_ID=%%a
    set INTERFACE_NAME=%%b
    rem 各インターフェースのトラフィックデータをCSVファイルに出力
    echo "Time,In,Out" > %%INTERFACE_ID%%.csv
    curl -s -H "X-Auth-Token: %AUTH_TOKEN%" %CACTI_URL%/api/v1/host.php?action=get_perfdata&id=%ROUTER_ID%&filter=if_id%3D%%INTERFACE_ID%%&if_id_value=%%INTERFACE_ID%%&start=%START_TIME%&end=%END_TIME%&output_format=csv | tail -n +2 | for /F "tokens=*" %%b in (response) do (
        echo %%b >> %%INTERFACE_ID%%.csv
    )
)

# 正規表現パターンとフォルダパスを設定
$pattern = '(ge_.*_.*_.*)(_.*)(.txt)'
$folderPath = '.\'

# フォルダ内の全ファイルを取得
Get-ChildItem -Path $folderPath -File | ForEach-Object {
    # ファイル名が正規表現に一致するか確認
    if ($_.Name -match $pattern) {
        # 一致した部分でファイル名を置き換え
#        $newName = $Matches[0]
        $newName = $Matches[1]+$Matches[3]
        $newPath = Join-Path -Path $_.Directory -ChildPath $newName

        # 新しいファイル名が既に存在するか確認
        if (-Not (Test-Path -Path $newPath)) {
            # ファイル名を変更
            Rename-Item -Path $_.FullName -NewName $newPath
        }
    }
}

#READ_ALL_CSV

@echo off
setlocal enabledelayedexpansion

REM 日時情報列番号の設定
set "DateColumn=2"

REM データ情報列番号を設定
set "DataColumn=5"

REM 入力ファイルから取り除く最初の行数
set "SkipLine=10"

REM result.csv ファイルを作成
copy nul ".\output\result.csv" /Y > nul

REM 最初のcsvファイルかフラグ
set "FileNo=1"

REM input フォルダ内のすべての CSV ファイルを処理
for /f "tokens=*" %%A in ('dir .\input\*.csv /b /o:n') do (
    set "FileName=%%~nA"
    set "FullFilePath=.\input\!FileName!.csv"
    REM ()内のファイル名指定にパス名を含められないため一時ファイルtmp.csvにコピー
    REM コピーの際に最初のSkipLine行を取り除く
    more +%SkipLine% "!FullFilePath!" > .\tmp.csv

    REM 最初のcsvファイルからだけ日時情報を読み出して結果ファイルの1行目に入れる
    if !FileNo!==1 (
        echo|set /p="Date,">> .\output\result.csv
        for /f "tokens=%DateColumn% delims=," %%B in (.\tmp.csv) do (
            echo|set /p="%%B," >> .\output\result.csv
        )
        echo.>> .\output\result.csv
    )

    echo|set /p="!FileName!,">> .\output\result.csv
    for /f "tokens=%DataColumn% delims=," %%B in (.\tmp.csv) do (
        echo|set /p="%%B," >> .\output\result.csv
    )
    echo.>> .\output\result.csv
    set /a "FileNo+=1"
)

endlocal
del .\tmp.csv /Q
pause

#PowerShellとPowerShell起動用バッチ

# Define the column numbers for date and data
$DateColumn = 1
$DataColumn = 3

# Define the number of lines to skip from the input file
$SkipLine = 10

# Create or overwrite the result.csv file
"" | Out-File -FilePath ".\output\result.csv" -Append -NoNewline -Encoding UTF8

# Initialize the file number flag
$FileNo = 1

# Process all CSV files in the input folder
Get-ChildItem ".\input\*.csv" | Sort-Object Name | ForEach-Object {
    $FileName = $_.BaseName
    $FullFilePath = ".\input\$FileName.csv"
    
    # Read the file content while skipping the first $SkipLine lines
    $CsvContent = Get-Content $FullFilePath | Select-Object -Skip $SkipLine

    # Only extract the date information from the first CSV file and append it to the result file's first line
    if ($FileNo -eq 1) {

        "Date," | Out-File -FilePath ".\output\result.csv" -Append -NoNewline -Encoding UTF8
        $CsvContent | ForEach-Object {
            $Row = ($_ -split ",")[$DateColumn-1]+","
            $Row | Out-File -FilePath ".\output\result.csv" -Append -NoNewline -Encoding UTF8
        }
        "" | Out-File -FilePath ".\output\result.csv" -Append -Encoding UTF8
    }
    
    # Append the file name and data information to the result file
    "$FileName," | Out-File -FilePath ".\output\result.csv" -Append -NoNewline -Encoding UTF8
    $CsvContent | ForEach-Object {
        $Row = ($_ -split ",")[$DataColumn-1]+","
        $Row | Out-File -FilePath ".\output\result.csv" -Append -NoNewline -Encoding UTF8
    }
    "" | Out-File -FilePath ".\output\result.csv" -Append -Encoding UTF8
    
    
    # Increment the file number
    $FileNo++
}
@echo off
powershell -ExecutionPolicy RemoteSigned -File ".\test.ps1"
pause
exit

VisualBasic版

Imports System.IO
Imports Microsoft.VisualBasic.FileIO

Module CSVProcessor
    Sub Main()
        Dim dateColumn As Integer = 0
        Dim dataColumn As Integer = 2
        Dim skipLines As Integer = 10
        Dim inputDir As String = ".\input"
        Dim outputFile As String = ".\output\result.csv"
        Dim isFirstFile As Boolean = True

        ' 出力ファイルを作成
        Directory.CreateDirectory(Path.GetDirectoryName(outputFile))
        Using sw As New StreamWriter(outputFile)
            ' inputディレクトリ内のすべてのCSVファイルを処理
            For Each filePath In Directory.GetFiles(inputDir, "*.csv")
                Using parser As New TextFieldParser(filePath)
                    parser.TextFieldType = FieldType.Delimited
                    parser.SetDelimiters(",")
                    parser.HasFieldsEnclosedInQuotes = True

                    ' 最初のskipLines行をスキップ
                    For i As Integer = 1 To skipLines
                        If Not parser.EndOfData Then
                            parser.ReadLine()
                        End If
                    Next

                    ' 最初のcsvファイルからだけ日時情報を読み出して結果ファイルの1行目に入れる
                    If isFirstFile Then
                        sw.Write("Date,")
                        While Not parser.EndOfData
                            Dim fields = parser.ReadFields()
                            If fields IsNot Nothing AndAlso fields.Length > dateColumn Then
                                sw.Write(fields(dateColumn) & ",")
                            End If
                        End While
                        sw.WriteLine()
                        isFirstFile = False
                    End If

                    ' ファイル名を書き出す
                    sw.Write(Path.GetFileNameWithoutExtension(filePath) & ",")

                    ' データ情報を抽出して結果ファイルに追加
                    parser.Close() ' 一度閉じて再度開く
                    Using parser2 As New TextFieldParser(filePath)
                        parser2.TextFieldType = FieldType.Delimited
                        parser2.SetDelimiters(",")
                        parser2.HasFieldsEnclosedInQuotes = True

                        ' 最初のskipLines行をスキップ
                        For i As Integer = 1 To skipLines
                            If Not parser2.EndOfData Then
                                parser2.ReadLine()
                            End If
                        Next

                        While Not parser2.EndOfData
                            Dim fields = parser2.ReadFields()
                            If fields IsNot Nothing AndAlso fields.Length > dataColumn Then
                                sw.Write(fields(dataColumn) & ",")
                            End If
                        End While
                    End Using
                    sw.WriteLine()
                End Using
            Next
        End Using
    End Sub
End Module

集計結果から最大値と最小値を出す

Imports System
Imports System.IO
Imports System.Data
Imports System.Text
Imports System.Collections.Generic
Imports Microsoft.VisualBasic.FileIO

Module Program
    Sub Main(args As String())
        Dim inputFolder As String = ".\input2"
        Dim outputFolder As String = ".\output2"

        If Not Directory.Exists(outputFolder) Then
            Directory.CreateDirectory(outputFolder)
        End If

        Dim files As String() = Directory.GetFiles(inputFolder, "*.csv")

        For Each file In files
            Dim dataTable As New DataTable()
            Using parser As New TextFieldParser(file)
                parser.TextFieldType = FieldType.Delimited
                parser.SetDelimiters(",")

                Dim firstLine As Boolean = True
                While Not parser.EndOfData
                    Dim fields As String() = parser.ReadFields()
                    If firstLine Then
                        For Each field As String In fields
                            dataTable.Columns.Add(New DataColumn(field, GetType(String)))
                        Next
                        firstLine = False
                    Else
                        dataTable.Rows.Add(fields)
                    End If
                End While
            End Using

            Dim columnSums(dataTable.Columns.Count - 1) As Double
            For i As Integer = 1 To dataTable.Columns.Count - 1
                For Each row As DataRow In dataTable.Rows
                    Dim value As String = row(i).ToString()
                    Dim number As Double
                    If Double.TryParse(value, number) Then
                        columnSums(i) += number
                    End If
                Next
            Next

            Dim totalRow As DataRow = dataTable.NewRow()
            totalRow(0) = "Total"
            For i As Integer = 1 To dataTable.Columns.Count - 1
                totalRow(i) = columnSums(i).ToString()
            Next
            dataTable.Rows.Add(totalRow)

            ' Total行の最大値と最小値を計算
            Dim maxTotal As Double = columnSums.Max()
            Dim minTotal As Double = Double.MaxValue
            For Each value As Double In columnSums
                If value > 0 AndAlso value < minTotal Then
                    minTotal = value
                End If
            Next

            ' 最大値と最小値の列のヘッダを取得
            Dim maxColumns As New List(Of String)
            Dim minColumns As New List(Of String)
            For i As Integer = 1 To dataTable.Columns.Count - 1
                If columnSums(i) = maxTotal Then
                    maxColumns.Add(dataTable.Columns(i).ColumnName)
                End If
                If columnSums(i) = minTotal Then
                    minColumns.Add(dataTable.Columns(i).ColumnName)
                End If
            Next

            ' MaxとMinの行を追加
            Dim maxRow As DataRow = dataTable.NewRow()
            maxRow(0) = "Max"
            maxRow(1) = String.Join(" ", maxColumns)
            maxRow(2) = maxTotal.ToString()
            dataTable.Rows.Add(maxRow)

            Dim minRow As DataRow = dataTable.NewRow()
            minRow(0) = "Min"
            minRow(1) = String.Join(" ", minColumns)
            minRow(2) = minTotal.ToString()
            dataTable.Rows.Add(minRow)

            Dim outputFilePath As String = Path.Combine(outputFolder, "Summed_" & Path.GetFileName(file))
            SaveCsv(dataTable, outputFilePath)
        Next
    End Sub

    Sub SaveCsv(dataTable As DataTable, filePath As String)
        Using writer As New StreamWriter(filePath, False, New UTF8Encoding(True))
            Dim titleRow As String = String.Join(",", dataTable.Columns.Cast(Of DataColumn)().Select(Function(col) col.ColumnName))
            writer.WriteLine(titleRow)

            For Each row As DataRow In dataTable.Rows
                Dim fields As String() = row.ItemArray.Select(Function(obj) obj.ToString()).ToArray()
                writer.WriteLine(String.Join(",", fields))
            Next
        End Using
    End Sub
End Module

最大値と最小値の算出まで1つのプログラムで行う

Imports System.IO
Imports System.Text
Imports Microsoft.VisualBasic.FileIO
Imports System.Collections.Generic

Module CSVProcessor
    Sub Main()
        Dim dateColumn As Integer = 0
        Dim dataColumn As Integer = 2
        Dim skipLines As Integer = 10
        Dim inputDir As String = ".\input"
        Dim outputFile As String = ".\output\result.csv"
        Dim isFirstFile As Boolean = True
        Dim columnTitles As New List(Of String)()
        Dim columnSums As New Dictionary(Of String, Double)()

        ' 出力ファイルを作成
        Directory.CreateDirectory(Path.GetDirectoryName(outputFile))
        Using sw As New StreamWriter(outputFile, False, Encoding.GetEncoding("shift_jis"))
            ' inputディレクトリ内のすべてのCSVファイルを処理
            For Each filePath In Directory.GetFiles(inputDir, "*.csv")
                Using parser As New TextFieldParser(filePath)
                    parser.TextFieldType = FieldType.Delimited
                    parser.SetDelimiters(",")
                    parser.HasFieldsEnclosedInQuotes = True

                    ' 最初のskipLines行をスキップ
                    For i As Integer = 1 To skipLines
                        If Not parser.EndOfData Then
                            parser.ReadLine()
                        End If
                    Next

                    ' 最初のcsvファイルからだけ日時情報を読み出して結果ファイルの1行目に入れる
                    If isFirstFile Then
                        sw.Write("Date,")
                        While Not parser.EndOfData
                            Dim fields = parser.ReadFields()
                            If fields IsNot Nothing AndAlso fields.Length > dateColumn Then
                                Dim title = fields(dateColumn)
                                sw.Write(title & ",")
                                columnTitles.Add(title)
                                columnSums(title) = 0
                            End If
                        End While
                        sw.WriteLine()
                        isFirstFile = False
                    End If

                    ' ファイル名を書き出す
                    sw.Write(Path.GetFileNameWithoutExtension(filePath) & ",")

                    ' データ情報を抽出して結果ファイルに追加
                    parser.Close() ' 一度閉じて再度開く
                    Using parser2 As New TextFieldParser(filePath)
                        parser2.TextFieldType = FieldType.Delimited
                        parser2.SetDelimiters(",")
                        parser2.HasFieldsEnclosedInQuotes = True

                        ' 最初のskipLines行をスキップ
                        For i As Integer = 1 To skipLines
                            If Not parser2.EndOfData Then
                                parser2.ReadLine()
                            End If
                        Next

                        Dim columnIndex As Integer = 0
                        While Not parser2.EndOfData
                            Dim fields = parser2.ReadFields()
                            If fields IsNot Nothing AndAlso fields.Length > dataColumn Then
                                Dim value As Double
                                If Double.TryParse(fields(dataColumn), value) Then
                                    sw.Write(value.ToString() & ",")
                                    If columnIndex < columnTitles.Count Then
                                        columnSums(columnTitles(columnIndex)) += value
                                    End If
                                End If
                                columnIndex += 1
                            End If
                        End While
                    End Using
                    sw.WriteLine()
                End Using
            Next

            ' 各列の合計値を追加
            sw.Write("Total,")
            For Each title In columnTitles
                sw.Write(columnSums(title).ToString() & ",")
            Next
            sw.WriteLine()

            ' 最大値とその列のタイトルを追加
            Dim maxValues = columnSums.Where(Function(pair) pair.Value = columnSums.Values.Max()).Select(Function(pair) pair.Key)
            sw.WriteLine("Max," & columnSums.Values.Max().ToString() & "," & String.Join(" | ", maxValues))

            ' 最小値とその列のタイトルを追加
            Dim minValues = columnSums.Where(Function(pair) pair.Value = columnSums.Values.Min()).Select(Function(pair) pair.Key)
            sw.WriteLine("Min," & columnSums.Values.Min().ToString() & "," & String.Join(" | ", minValues))
        End Using
    End Sub
End Module
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?