snipe / snipe-it

A free open source IT asset/license management system
https://snipeitapp.com
GNU Affero General Public License v3.0
11.12k stars 3.19k forks source link

Next Audit Date set to NULL when auditing via API #8456

Open joepitt91 opened 4 years ago

joepitt91 commented 4 years ago

Please confirm you have done the following before posting your bug report:

Describe the bug When auditing assets via POST to the API endpoint /api/v1/hardware/audit the last audit date and location are correctly recorded, however, the next audit date is set to NULL, with nothing logged in storage/logs/laravel.log, including with Advanced Debugging APP_DEBUG=true enabled.

To Reproduce Steps to reproduce the behavior:

  1. Go to '/hardware/360' and note audit time and next audit time.
  2. Send a POST to the /api/v1/hardware/audit Endpoint with the asset_tag and location_id values set in the body
  3. Go back to /hardware/360 and notice audit time has updated and next audit time is gone.
  4. Audit the same asset via Bulk Audit page,
  5. Go back to /hardware/360 and notice audit time has updated and next audit time is back.

Expected behavior The next audit date should be set as per it is stated as being set in the API response body.

Screenshots

API response

{
    "status": "success",
    "messages": "Asset audit successfully logged.",
    "payload": {
        "asset_tag": "1234",
        "note": "",
        "next_audit_date": {
            "datetime": "2021-09-19 00:00:00",
            "formatted": "19.09.2021 00:00"
        }
    }
}

Server (please complete the following information):

Desktop (please complete the following information):

Error Messages

No errors, API Response is HTTP 200, and says next audit has been set.

Additional context

joepitt91 commented 4 years ago

Correction, auditing via the API with location_id set does not update the location of the asset either.

boring10 commented 4 years ago

Out of curiosity, do you have an interval set under your "Notification" settings?

As for the location. The location does not update, at this time, when you enter a location for the audit. The current location field is it just make a note of it's current location when the audit was performed.

It does appear that in version 5 that there is a checkbox option to update the location but I don't know if it will be available in the initial v5 release or not.

Screenshot from 2020-09-27 06-57-54

joepitt91 commented 4 years ago

On notifications page, I have these audit settings:

Auditing via WebUI sets next audit to 24 months time.

Ref location, I'm now doing calls to these endpoints to audit, get ID of and then move the assets:

stale[bot] commented 3 years ago

Is this still relevant? We haven't heard from anyone in a bit. If so, please comment with any updates or additional detail. This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Don't take it personally, we just need to keep a handle on things. Thank you for your contributions!

matreicht commented 5 months ago

Same issue here, since bulk audit on webui is designed for barcode scanners and multiple devices cannot be audited the same time, workaround would be via api but this is kinda flawed

Edit: You can use put method to change the next audit date to whatever your policy defines. I wrote a python script that reads asset tags from a file, audits them one by one then sets the next audit date, for me its 1 year from the current date. If anyone's in the same shoes let me know and i will share the script

stale[bot] commented 5 months ago

Okay, it looks like this issue or feature request might still be important. We'll re-open it for now. Thank you for letting us know!

jonbloom commented 4 weeks ago

Same issue here, since bulk audit on webui is designed for barcode scanners and multiple devices cannot be audited the same time, workaround would be via api but this is kinda flawed

Edit: You can use put method to change the next audit date to whatever your policy defines. I wrote a python script that reads asset tags from a file, audits them one by one then sets the next audit date, for me its 1 year from the current date. If anyone's in the same shoes let me know and i will share the script

I'd love to see what you came up with -- I'm working on a project to normalize audit dates by each location and this could be pretty useful to look at.

matreicht commented 3 days ago

here you go:

import requests
import json
from datetime import datetime, timedelta
import time

#THIS SCRIPT AUDITS IN SNIPEIT, SETS AUDIT NOTE TO: "yournote"  AND SETS NEXT AUDIT DATE TO + 1 YEAR, READS ASSET_TAGS FROM FILE

# Variables
AUDIT_API_URL = 'https://yoursnipeit.url/api/v1/hardware/audit'
ASSET_API_URL = 'https://yoursnipeit.url/api/v1/hardware'
API_KEY = 'yourapikey'
# Getting asset_tags from a file
def read_asset_tags(file_path):
    with open(file_path, 'r') as file:
        return [line.strip() for line in file.readlines()]

# Defining path to file
ASSET_TAGS_FILE = 'asset_tags.txt'

# Making a list of asset_tags
ASSET_TAGS = read_asset_tags(ASSET_TAGS_FILE)

# Create the headers for the request
headers = {
    'Authorization': f'Bearer {API_KEY}',
    'Content-Type': 'application/json'
}

for asset_tag in ASSET_TAGS:
    print(f"Processing asset tag: {asset_tag}")

    # Create the body of the audit request
    audit_data = {
        'name': f'Audit for {asset_tag}',
        'asset_tag': asset_tag,
        'type': 'audit',
        'action': 'audit',
        'note': 'yournote'
    }

    # Send the POST request to create an audit record
    audit_response = requests.post(AUDIT_API_URL, headers=headers, json=audit_data)

    # Check the response
    if audit_response.status_code == 200:
        print(f'Audit record created successfully for asset tag {asset_tag}.')
    else:
        print(f'Failed to create audit record for asset tag {asset_tag}. Status code: {audit_response.status_code}')
        continue  # Skip to the next asset tag if auditing fails

    # Wait for 1 second
    time.sleep(1)

    # Calculate the next audit date (current date + 1 year)
    next_audit_date = (datetime.now() + timedelta(days=365)).strftime('%Y-%m-%d')

    # Get the asset ID by asset tag
    assets_response = requests.get(f'{ASSET_API_URL}?search={asset_tag}', headers=headers)

    # Check the response
    if assets_response.status_code == 200:
        assets = assets_response.json()
        if assets['total'] > 0:
            asset_id = assets['rows'][0]['id']
            print(f'Found asset ID {asset_id} for asset tag {asset_tag}.')
        else:
            print(f'Asset not found for asset tag {asset_tag}.')
            continue  # Skip to the next asset tag if the asset is not found
    else:
        print(f'Failed to retrieve asset information for asset tag {asset_tag}. Status code: {assets_response.status_code}')
        continue  # Skip to the next asset tag if retrieving asset information fails

    # Wait for 1 second
    time.sleep(1)

    # Create the body of the update request
    update_data = {
        'next_audit_date': next_audit_date
    }

    # Send the PUT request to update the next audit date
    update_response = requests.put(f'{ASSET_API_URL}/{asset_id}', headers=headers, json=update_data)

    # Check the response
    if update_response.status_code == 200:
        print(f'Next audit date set to {next_audit_date} for asset tag {asset_tag}.')
    else:
        print(f'Failed to set next audit date for asset tag {asset_tag}. Status code: {update_response.status_code}')