Fault handling in cache

Hi,
is it possible to delete faulty tasks from cache? I accidently send a download request to a lot of CPE´s with a empty filename. Now I have 8k faults in queue. Even deleting them from mongodb they appear again. What can I do to get rid of this faults (delete them)? How does genieacs handle faults in cache?

Please don’t directly modify Mongo. As you’ve discovered, GenieACS caches things. You can this php script I have and modify it to suite your needs to delete the faulty tasks. In its current implementation, the script is programmed to delete tasks older than 30 days.

You can easily change this line $gpv = $row['name'] === 'getParameterValues' && $row['parameterNames'][0] === 'InternetGatewayDevice.ManagementServer.PeriodicInformInterval'; to be have the task name be download and the you will have to figure out the second condition to match the bad file path you sent.

Again, this is a starting point that should get you 95% of the way to an automated solution to delete those faulty tasks.

<?php

$nbi = 'https://YOUR_GENIEACS_HOSTNAME_OR_IP:7557';

$tasks = getStaleTasks($nbi);
$count = count($tasks);

printf("Deleting %d tasks\r\n", $count);

foreach ($tasks as $task) {
    $uri = sprintf('%s/tasks/%s', $nbi, $task['_id']);
    printf("%s\r", $uri);

    if (!deleteTask($uri)) {
        print("Error deleting task ${task['_id']}\r\n");
    }
}

printf("Deleted %d tasks\r\n\r\n", $count);

function deleteTask($uri): bool {
    print("${uri}\r\n");
    $ch = createCurl($uri);

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
    curl_exec($ch);

    $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    
    return $statusCode === 200;
}

function getStaleTasks($nbi) {
    $ch = createCurl(sprintf('%s/tasks', $nbi));

    $data = json_decode(curl_exec($ch), true);

    curl_close($ch);

    $now = new DateTime('now');

    return array_filter($data, static function (array $row) use($now) {
        $date = new DateTime($row['timestamp']);

        $old = $date->diff($now)->days > 30;
        $gpv = $row['name'] === 'getParameterValues' && $row['parameterNames'][0] === 'InternetGatewayDevice.ManagementServer.PeriodicInformInterval';

        return $old | $gpv;
    });
}

function createCurl(string $url) {
    $result = curl_init();

    curl_setopt($result, CURLOPT_TIMEOUT_MS, 20000);
    curl_setopt($result, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($result, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($result, CURLOPT_URL, $url);

    return $result;
}

Thank you for responding. I´ve modified your script to delete faults. nbi does respond with 200 OK but the faults are not deleted. Do you know why? Is it possible to delete faults via nbi?

<?php



$nbi = 'http://127.0.0.1:7557';



$faults = getStaleFaults($nbi);
$count = count($faults);



printf("Deleting %d faults\r\n", $count);



foreach ($faults as $fault) {

    $uri = sprintf('%s/faults/%s', $nbi, $fault['_id']);

    printf("Deleting %s \r\n", $uri);

    if (!deleteFaults($uri)) {

        print("Error deleting fault ${fault['_id']}\r\n");

    }


}



printf("Deleted %d faults\r\n\r\n", $count);



function getStaleFaults($nbi) {

    $ch = createCurl(sprintf('%s/faults', $nbi));

    $data = json_decode(curl_exec($ch), true);


    curl_close($ch);



    $now = new DateTime('now');



    return array_filter($data, static function (array $row) use($now) {

        $date = new DateTime($row['timestamp']);

        return $date;

    });

}


function deleteFaults($uri) {

    print("${uri}\r\n");

    $ch = createCurl($uri);


    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');

    print_r (curl_exec($ch));



    $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);


    return $statusCode === 200;

}


function createCurl($url) {

    $result = curl_init();



    curl_setopt($result, CURLOPT_TIMEOUT_MS, 20000);

    curl_setopt($result, CURLOPT_SSL_VERIFYPEER, true);

    curl_setopt($result, CURLOPT_RETURNTRANSFER, true);



    curl_setopt($result, CURLOPT_URL, $url);



    return $result;

}

You are not url encoding the ID…

I’ve cleaned up the code (sorry, its the programmer in me) and added the missing url encode to the fault id.

<?php

$nbi = 'http://localhost:7557';

$faults = getFaults($nbi);

printf("Deleting %d faults\r\n", count($faults));

$deleted = 0;

foreach ($faults as $fault) {
    $uri = sprintf('%s/faults/%s', $nbi, rawurlencode($fault['_id']));

    printf("Deleting %s \r\n", $uri);

    if (deleteFault($uri)) {
        ++$deleted;
    } else {
        print("Error deleting fault ${fault['_id']}\r\n");
    }
}

printf("Deleted %d faults\r\n\r\n", $deleted);

function getFaults($nbi) {
    $ch = createCurl(sprintf('%s/faults', $nbi));
    $data = json_decode(curl_exec($ch), true);
    curl_close($ch);

    return $data;
}

function deleteFault($uri) {
    $ch = createCurl($uri);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
    curl_exec($ch);

    return curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200;
}

function createCurl($url) {
    $result = curl_init();

    curl_setopt($result, CURLOPT_TIMEOUT_MS, 20000);
    curl_setopt($result, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($result, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($result, CURLOPT_URL, $url);

    return $result;
}

now I know the difference between urlencode() and rawurlencode() :slight_smile: Thank you very much its working.

Ahh yes, that one took me a bit to figure out as well…