Splunk Dev

How edit my code to display the results of a file in Splunk using Django bindings?

Federica_92
Communicator

Hi everyone,

I'm trying to display the results of a file in Splunk using the django bindings, but something is wrong in my code. Could someone help me?

URL.py

url(r'^mypage/$', 'mynewapp.views.mypage_view', name='mypage'),

views.py

@render_to('mynewapp:mypage.html')
@login_required
def mypage_view(request):
file_data = ''
service = request.service
with open(csvfile, "r+") as lines:      
for line in lines:
    file_data += line
     return file_data

mypage.html

   {% for data in file_data %}
    <p>{{ data }}</p>
    {% endfor %}
0 Karma

lmyrefelt
Builder

OK, you have to excuse me for being a newbie at all these things.

However ... what do you want to accomplish ?
Do you want to create /render a "view" generate some output into a splunk template?

Are you trying to connect to splunk over its python api/sdk stuff , list all the csv files , save it into file_data ?

From what i can see you seems to be trying to "open" a file locally and loop it line by line.

if so your code is off in my eyes ... and did not work locally for me either ...

Based on ;
http://answers.splunk.com/answers/216282/how-to-write-a-search-to-display-all-names-of-look.html

i would think you need to start like;
service = request.service
lookups = service.data.table-files

and then you want to either filter / looking for the "correct" lookup/table-file before your go over it line by line ... makes sense ?

0 Karma

lmyrefelt
Builder

so maybe you need to start with a search manager ?

0 Karma

lmyrefelt
Builder

and if you want to open a file locally , from filessytem, you might have to import som 'e extra functions into views.py

The following lists content in a dir
views.py
from os import listdir
from os.path import isfile, join

...

@render_to('test:listfilesdemo.html')
@login_required
def listfilesdemo_view(request):
        files = listdir("/tmp/")
        return {
        "files": files
}

then in your template;
{{ files }}

as a start

0 Karma

Federica_92
Communicator

Good morning : ) Don't worry, I'm newbie too.

I have open and loop the file, but I have still some problem.
My template ask me to choose an option from a dropdown menu, when you select the option, it generate a external lookuptable and my views.py edit it.
The problem is that the view, try to make this before that I choose my option in the template, so I would like insert an if condition before the render_to(..) to be sure that this is execute only AFTER the selection in the dropdown menu.
And my second problem is that the views open the csv file (lookuptable) But I had change the permission of the file. I would like that the file that splunk generate are accessible to everyone, or at least, at my script.
Have you solve the yesterday's problem?
Please, let me know,
thank you!

0 Karma

lmyrefelt
Builder

It seems i might have deleted your comment after i deleted my to correct a mistake i made ...

Maybe the setup form can be of help to you there seems to be some if checks going on there ?

http://dev.splunk.com/view/SP-CAAAETW

0 Karma

Federica_92
Communicator

Yeah, I have look at this, but it' s not what I'm trying to do : ( and furthermore splunk framework kick my ass every five minutes for every type of error that is not an error, but for him, is an error.

0 Karma

lmyrefelt
Builder

Good morning to you too.
Well i think you have some more question / issues other than the original one (asked here) ?
Your "original" problem looked quiet similar to mine, That is my "views" / "template" was not rendered and or showed me only a ;

404 Not Found
Return to Splunk home page
 The path '/en-US/test/' was not found.
 View more information about your request (request ID = 54edb5d1b47f93cc64a3d0) in Search

 This page was linked to from http://10.0.1.21:8000/en-US/app/test/default.

This message comes up when your code inside views.py / render_to , are screwed up. This can either be bad code or the wrong formatting .. remember python is kinda of nazi about the code formatting.

Otherwise in the example you have given above your for-loop in your template looks good.

Based on your comment this morning, this question here is resolved (by yourself?) ?

I have solved my problems i had yesterday, which was based on "bad code formatting" inside views.py, and due to not really understand the templates > urls > views connections better.

Since then i have attempted to run some external / custom scripts etc and generate the results back into the template .
Which i have seen to come around by putting the following inside views.py;

import sys
sys.path.append('modules/')
import listFiles

Which makes it possible for you to create your own modules and load them into "splunk / django"

0 Karma

lmyrefelt
Builder

and the following seems to be due to an missing render_to stanza in views_py

This XML file does not appear to have any style information associated with it. The document tree is shown below.
0 Karma

lmyrefelt
Builder

About your if-statement look here; http://www.djangobook.com/en/2.0/chapter09.html

0 Karma

lmyrefelt
Builder

You do have a lot of interesting questions that it is going to be educating to follow 🙂

0 Karma

lmyrefelt
Builder

so we are in the same boat (at least now:P ) ok ..
(the comments are to short for me to post this ... hopefully helpful for someone )
(for people coming in and reading)

I have created a new page timedemo.html , which can contain any content, or without .
I edited urls.py

 rlpatterns = patterns('',
        url(r'^home/$', 'test.views.home', name='home'),
        url(r'^pythondemo/$', 'test.views.pythondemo_view', name='pythondemo'),
        url(r'^timedemo/$', 'test.views.timedemo_view', name='timedemo'),
    )

With a simple yank and paste .

Restarted splunk. Trying to access my new app "test" .. only to get this;

404 Not Found

Return to Splunk home page
The path '/en-US/test/' was not found.
View more information about your request (request ID = 54edb5d1b47f93cc64a3d0) in Search

This page was linked to from http://10.0.1.21:8000/en-US/app/test/default.

Browsing / typing my old urls "home, pythondemo" from adressfield works fine.
When trying to browse my newly created page; timedemo .. this is what i get back;

 This XML file does not appear to have any style information associated with it. The document tree is shown below.
      <response><messages><msg type="ERROR">INTERNAL SERVER ERROR</msg></messages></response>

in gjango_error.log i find;

Traceback (most recent call last): File "/opt/splunk/dev/6.2.1/splunk/lib/python2.7/site-packages/django/core/handlers/base.py", line 103, in get_response resolver_match = resolver.resolve(request.path_info) File "/opt/splunk/dev/6.2.1/splunk/lib/python2.7/site-packages/django/core/urlresolvers.py", line 321, in resolve sub_match = pattern.resolve(new_path) File "/opt/splunk/dev/6.2.1/splunk/lib/python2.7/site-packages/django/core/urlresolvers.py", line 321, in resolve sub_match = pattern.resolve(new_path) File "/opt/splunk/dev/6.2.1/splunk/lib/python2.7/site-packages/django/core/urlresolvers.py", line 321, in resolve sub_match = pattern.resolve(new_path) File "/opt/splunk/dev/6.2.1/splunk/lib/python2.7/site-packages/django/core/urlresolvers.py", line 223, in resolve return ResolverMatch(self.callback, args, kwargs, self.name) File "/opt/splunk/dev/6.2.1/splunk/lib/python2.7/site-packages/django/core/urlresolvers.py", line 230, in callback self._callback = get_callable(self._callback_str) File "/opt/splunk/dev/6.2.1/splunk/lib/python2.7/site-packages/django/utils/functional.py", line 31, in wrapper result = func(*args) File "/opt/splunk/dev/6.2.1/splunk/lib/python2.7/site-packages/django/core/urlresolvers.py", line 118, in get_callable (lookup_view, mod_name)) ViewDoesNotExist: Could not import test.views.timedemo_view. View does not exist in module test.views.

Could not import test.views.timedemo_view. View does not exist in module test.views. 

So i set up some empty views in view.py and now i am not seing these messages anymore ... pain in the ass to have to this for all the pages / templats i setup.

Which seems to be necessary even if i am not doing anything special in the template ...

It seems to be some things i have not yet understoud regarding django.

0 Karma

Federica_92
Communicator

Could you paste your render function too? Maybe help

0 Karma

lmyrefelt
Builder

Do you mean my views.py ?

@render_to('test:timedemo.html')
@login_required
def timedemo_view(request):
        return {
}
0 Karma

Federica_92
Communicator

Any news?

0 Karma

lmyrefelt
Builder

do you have { and } in your return ?

0 Karma

Federica_92
Communicator
return {
    "file_data":file_data
     }

Like this : )

0 Karma

Federica_92
Communicator

I have use the demo, that I have found on the splunk page tutorial, it work to display the data, but If I change the name at the view functions or at the url, like I have done over here, anything doesn't work : (

My first problem is take the results from {{data}} and elaborate them.
And take results of the dropdown menu and the checkbox and output them in an external file.
I have try to ask, but you are the first one that are replying to me.

0 Karma

lmyrefelt
Builder

It is the same problem i have ... my point underneath here is that web framework toolkit app that you can download from apps.splunk.com that i would assume would have used web framework / django bindings for its views and urls looks different from the examples (in the doc) and the produced code from generating an web framework app.

Hopefully i have the energy to dig in deeper today

0 Karma

Federica_92
Communicator

Yeah, I'm using it, let me know if you found something, thank you : )

0 Karma

lmyrefelt
Builder

F* me, i am also struggling with this one ...

I can see that in urls.py from the framework toolkit that you can download from apps.splunk.com their rendering looks a little bit different;

urlpatterns = patterns('',
    url(r'^home/$', render('splunk_wftoolkit:home.html'), name='home'),
    url(r'^overview/$', render('splunk_wftoolkit:overview.html'), name='overview'),
    # Components
    url(r'^managers/$', render('splunk_wftoolkit:managers.html'), name='managers'),
    url(r'^charts/$', render('splunk_wftoolkit:charts.html'), name='charts'),
    url(r'^tables/$', render('splunk_wftoolkit:tables.html'), name='tables'),
    url(r'^forms/$', render('splunk_wftoolkit:forms.html'), name='forms'),
    url(r'^dataview/$', render('splunk_wftoolkit:dataview.html'), name='dataview'),
    url(r'^searchcontrols/$', render('splunk_wftoolkit:searchcontrols.html'), name='searchcontrols'),
    url(r'^map/$', render('splunk_wftoolkit:map.html'), name='map'),

Lets see what i can make happen tomorrow

0 Karma
Get Updates on the Splunk Community!

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...

What's new in Splunk Cloud Platform 9.1.2312?

Hi Splunky people! We are excited to share the newest updates in Splunk Cloud Platform 9.1.2312! Analysts can ...

What’s New in Splunk Security Essentials 3.8.0?

Splunk Security Essentials (SSE) is an app that can amplify the power of your existing Splunk Cloud Platform, ...